Last post Nov 23, 2012 04:51 AM by jwezel
Sep 09, 2011 04:21 AM|ashok.bohra|LINK
I am trying to make a GET webrequest to a REST API. My URL to make a call is something like
but when I make the request, the call is made with the URL
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();
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.
Sep 09, 2011 08:33 AM|gerrylowry|LINK
i do not know the answer. I did notice that Google replaced
%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?
Sep 09, 2011 08:44 AM|gerrylowry|LINK
I just tried %252f as a search argument in Google. A number of interesting results are returned.
For example, this security article:
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).
Sep 09, 2011 04:17 PM|rossmc1|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
Sep 12, 2011 02:08 AM|ashok.bohra|LINK
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
Sep 12, 2011 02:36 AM|gerrylowry|LINK
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.
Nov 23, 2012 04:51 AM|jwezel|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)
''' 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
Private Class UriWithCorrectDataEncoding
Public Sub New(url As String)
Public Overrides Function ToString() As String