I get the error at GetREaponse call as the REST api is expecting %2f instead of /
I have tried double encoding but that converts the %2f to %252f. And when the call is made the %252f is not converted to %2F and call is made as it. This again leads to an error.
There used to be an overloaded constructor for URI wherein we can pass the "bool dontEscape" parameter for stopping the .Net from escaping the string. But this seems to have been deprecated and is now false by default.
I have also tried the WebResponse.Create (String URL) method but the result is same.
i do not know the answer. I did notice that Google replaced
Ashok%2fAshok%2fAshok with
Ashok%252fAshok%252fAshok
%2f is the ASCII code for the forward slask and the '/' is very important as a separator in URLs ... so it might be not a .NET problem but a restriction imposed by the design of the internet.
Fiddler2 (http://fiddler2.com) shows me that the
GET sent to Google had the %252f:
GET /search?hl=en&source=hp&biw=1067&bih=457&q=Ashok%252fAshok%252fAshok ...
QUESTION: why are you sending a forward slash as a %2f in your string ... do you actually need it?
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
QUESTIONS: where are you seeing the '/' ... are you seeing it in Visual Studio? ... if you are, then visual studio is likely just trying to be helpful.
what is getting sent? (you can use Fiddler2 to check that out).
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
THanks for the reply. Well the requirement if from the third party that we are trying to integrate with and they require "Signature" to be URI encoded.
As for the visual Studio when I debug I see that
WebRequest.RequestURI property has "/" in the value but the property
WebRequest.RequestUri.OriginalString has "%2f" in the value so this means that visual studio is actually converting the %2f to / and also I found the following article
The suggestion by rossmc1 might work in some cases; I suspect that it will not work in yours becauae a third party is involved. (I could be wrong.) It's probably worth some of your time to
experiment with rossmc1's suggestion.
Also, debugging in vs is good BUT it's still useful to watch your traffic with Fiddler2 because what you see in vs debug is not always 100% compatible with what happens to your data on its way through the framework.
One more suggestion: talk to the third party ... perhaps the third party has helped others solve your problem in the past and can give you some direction.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
use e.g. following code (attention: works only for absolute URLs!):
Sub DownloadFileToDisc (downloadUrl As String)
Dim MyWebClient As New System.Net.WebClient()
Dim downloadUri As New UriWithCorrectDataEncoding(downloadUrl)
MyWebClient.DownloadFile(downloadUri, localFilePath)
End Sub
''' <summary>
''' Original behaviour of System.Uri.ToString returns unescaped characters in query parameters - a behaviour which corrupts URLs like http://domain.com/?forwardurl=module%3dInvoice%26action%3dCreatePDF4Mail%26return_module%3dInvoice into http://domain.com/?forwardurl=module=Invoice&action=CreatePDF4Mail&return_module=Invoice
''' </summary>
''' <remarks></remarks>
Private Class UriWithCorrectDataEncoding
Inherits System.Uri
Public Sub New(url As String)
MyBase.New(url)
End Sub
Public Overrides Function ToString() As String
Return MyBase.AbsoluteUri
End Function
ashok.bohra
Member
2 Points
2 Posts
Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 09, 2011 08:21 AM|LINK
I am trying to make a GET webrequest to a REST API. My URL to make a call is something like
http://someting/something/therequest+SJiHaYysSq8N1e0vg7798Pp9aBN%2fpqU9VcowJ7HvN18
but when I make the request, the call is made with the URL
http://someting/something/therequest+SJiHaYysSq8N1e0vg7798Pp9aBN/pqU9VcowJ7HvN18
notice the difference between the two URL’s %2f gets converted to /
How to stop the .Net from doing this. My code is
Uri reqURI = new Uri(uploadOoyalaAPI + "pcode=" + pcode + query + signature);
WebRequest request = WebRequest.Create(reqURI);
XmlDocument xmldoc = new XmlDocument();
string result = null;
WebResponse resp = request.GetResponse(); // getting the error here
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
xmldoc.LoadXml(result);
I get the error at GetREaponse call as the REST api is expecting %2f instead of /
I have tried double encoding but that converts the %2f to %252f. And when the call is made the %252f is not converted to %2F and call is made as it. This again leads to an error.
There used to be an overloaded constructor for URI wherein we can pass the "bool dontEscape" parameter for stopping the .Net from escaping the string. But this seems to have been deprecated and is now false by default.
I have also tried the WebResponse.Create (String URL) method but the result is same.
Any help would be appreciated.
Thanks
Ashok
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 09, 2011 12:33 PM|LINK
http://www.asciitable.com/
i do not know the answer. I did notice that Google replaced
Ashok%2fAshok%2fAshok with
Ashok%252fAshok%252fAshok
%2f is the ASCII code for the forward slask and the '/' is very important as a separator in URLs ... so it might be not a .NET problem but a restriction imposed by the design of the internet.
Fiddler2 (http://fiddler2.com) shows me that the GET sent to Google had the %252f:
GET /search?hl=en&source=hp&biw=1067&bih=457&q=Ashok%252fAshok%252fAshok ...
QUESTION: why are you sending a forward slash as a %2f in your string ... do you actually need it?
g.
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 09, 2011 12:44 PM|LINK
I just tried %252f as a search argument in Google. A number of interesting results are returned.
For example, this security article: https://www.owasp.org/index.php/Double_Encoding
This too: http://drupal.stackexchange.com/questions/10736/252f-in-the-url-path
QUESTIONS: where are you seeing the '/' ... are you seeing it in Visual Studio? ... if you are, then visual studio is likely just trying to be helpful.
what is getting sent? (you can use Fiddler2 to check that out).
g.
rossmc1
Member
644 Points
152 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 09, 2011 08:17 PM|LINK
just process the get the part of the url manually ,
i think applying on of those functions to the url string will fix the issue
HttpUtility.UrlEncode
HttpUtility.UrlDecode
ashok.bohra
Member
2 Points
2 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 12, 2011 06:08 AM|LINK
Hi Gerry,
THanks for the reply. Well the requirement if from the third party that we are trying to integrate with and they require "Signature" to be URI encoded.
As for the visual Studio when I debug I see that
WebRequest.RequestURI property has "/" in the value but the property
WebRequest.RequestUri.OriginalString has "%2f" in the value so this means that visual studio is actually converting the %2f to / and also I found the following article
http://connect.microsoft.com/VisualStudio/feedback/details/94109/system-uri-constructor-evaluates-escaped-slashes-and-removes-double-slashes
Thanks
Ashok
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Sep 12, 2011 06:36 AM|LINK
@ ashok.bohra
you're welcome.
The suggestion by rossmc1 might work in some cases; I suspect that it will not work in yours becauae a third party is involved. (I could be wrong.) It's probably worth some of your time to experiment with rossmc1's suggestion.
Also, debugging in vs is good BUT it's still useful to watch your traffic with Fiddler2 because what you see in vs debug is not always 100% compatible with what happens to your data on its way through the framework.
One more suggestion: talk to the third party ... perhaps the third party has helped others solve your problem in the past and can give you some direction.
g.
jwezel
Member
32 Points
7 Posts
Re: Stopping .Net from Escaping the ALready URIencoded Query String when creating a Web request
Nov 23, 2012 08:51 AM|LINK
use e.g. following code (attention: works only for absolute URLs!):
Sub DownloadFileToDisc (downloadUrl As String)
Dim MyWebClient As New System.Net.WebClient()
Dim downloadUri As New UriWithCorrectDataEncoding(downloadUrl)
MyWebClient.DownloadFile(downloadUri, localFilePath)
End Sub
''' <summary>
''' Original behaviour of System.Uri.ToString returns unescaped characters in query parameters - a behaviour which corrupts URLs like http://domain.com/?forwardurl=module%3dInvoice%26action%3dCreatePDF4Mail%26return_module%3dInvoice into http://domain.com/?forwardurl=module=Invoice&action=CreatePDF4Mail&return_module=Invoice
''' </summary>
''' <remarks></remarks>
Private Class UriWithCorrectDataEncoding
Inherits System.Uri
Public Sub New(url As String)
MyBase.New(url)
End Sub
Public Overrides Function ToString() As String
Return MyBase.AbsoluteUri
End Function
End Class