HttpWebRequest truncating URL with non-english symbols in it - c#

I'm creating HttpWebRequest with a dynamic URL that can sometimes contain Russian symbols.
And sometimes I'm encounter an issue I not sure how to deal with: While encoding to URL each of RU characters has been converted into URL character-entity equivalents (%20...). Apparently after such conversion URL is becoming too long so HttpWebRequest truncating few last characters so instead of proper HTTP result I'm getting 404 error.
Is there any way to bypass this limitation?
My URL looks like this:
http:\\1.1.1.1\some?page=2&var=тестовое значение строки (this part can be very long)
I'm sending this as a GET since web-app I'm working with is expecting GET request at this point.

The only answer to this is you have to send it in any other way as there is a limit for the length of a query string.
The error 404 or "HTTP Error 414. The request URL is too long." you'll get only because of the length of the query string as the data you are sending is longer than the limit.
refer this: http://forums.asp.net/t/1139751.aspx

Related

How to encode IP for GET request in api in .NET

I'm trying to figure out how to send IP in GET request. I want to call GET request like : /api/endpoint/12.12.12.12. I tried to encode it but HttpUtility.UrlEncode won't encode dots for IP alone. When I try use %2E as dot then IIS throws 404.11 - The request filtering module is configured to deny a request that contains a double escape sequence.. How to I make it the right way?
Try to encode it in base 64. You can find how to do it here
/api/endpoint/MTIuMTIuMTIuMTI=
You could just do a string replace.
"12.12.12.12".Replace(".","%2E");
Add a slash at the end of the URL:
/api/endpoint/12.12.12.12/
this should work

C# WebAPI - getting URL with parameters passed as QueryString

I've been searching for this problem but non was identical to my case.
I have the following controller:
public HttpResponseMessage GetMyService(int aType, [FromUri] string streamURL)
streamURL is a parameter that gets a full URL sent by the client.
The client calls the service like that:
http://www.myservice.com/.../GetMyService/?aType=1&streamURL=http://www.client.com/?p1=100&p2=200
The problem is that at then end, I get the [FromUri] string streamURL parameter as http://www.client.com/?p1=100 without the &p2=200
This is known and reasonable, but I cannot place any encoding/decoding functionality as the URL is cut at the very beginning.
Any help would be appreciated..
THX
The client should properly URL encode the value of the streamURL query string parameter when making the request in order to conform to the HTTP protocol specification:
http://www.myservice.com/.../GetMyService/?aType=1&streamURL=http%3A%2F%2Fwww.client.com%2F%3Fp1%3D100%26p2%3D200
So basically there's nothing you could do on the server side, you should fix the client.

Character Encoding causes my ASP Web API request to fail with HTTP 400: Bad Request

I have a json object which I serialize and post. I found that if one of the string members of my json object has a few unicode characters such as é, ó, 6̄ (I have some other unicode characters that �犞ݮ do not return an HTTP 400 error) that the request returns HTTP 400 Bad Request.
Here is the simplified code for how the call is being made:
WebClient client;
Foo myObject = new Foo();
myObject.StringField1 = "é";
string serializedObjects = Newtonsoft.Json.JsonConvert.SerializeObject(myObject)
client.Headers[HttpRequestHeader.ContentType] = "application/json;charset=utf-8";
var response = client.UploadString(url, serializedObjects);
Here is the code for how the calls is being received on server side:
public async Task<IHttpActionResult> Post([FromBody] IList<Foo> myObjects){}
Here are some things that I have researched/tried and some assumptions I have made:
1) Get the string object as UTF8 bytes, then transform it back to a string
Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(serializedObjects))
2) When I make the request and capture the traffic using Fiddler and inspect the request in Raw. For the unicode characters that don't return a 400 error they are replaced with a '?' however for the ones that do cause a 400 they are displayed as a blank. However when I look at JSON view it shows as a null character '�'
3) HTTP 400 is returned is ASP Web API can't deserialize the object correctly, so I tried using Newtonsoft library to serialize and deserialize the object and this works just fine. (The default serializer for ASP Web API is JSON.NET so it should be performing the same serialization http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization)
string b = JsonConvert.SerializeObject(a);
Foo c = (Foo) JsonConvert.DeserializeObject(b, typeof(Foo));
// And I tried deserializing using the generic method
Foo d = JsonConvert.DeserializeObject<Foo>(b);
4) I was convinced that this was an issue with my server rejecting these special characters, but when I used Fiddler to retry the request (inserting the é back in the place where Fiddler had removed it from in the original request) the call succeeds.
5) I validated my Content-Type on http://www.iana.org/assignments/media-types/media-types.xhtml and also specify the utf8 charset explicitly.
So this seems to me to be an encoding issue, but é should be a valid UTF-8 character. I validated it in Jon Skeet's Unicode Explorer widget on his page. So I'm out of ideas for why this is causing issues...
As #roeland and #dbc pointed out there is an Encoding property on the WebClient. Setting this to Encoding.UTF8 resolved the issue.
client.Encoding = Encoding.UTF8;

WebBrowser keep url/uri encoded dont decode

I have a web-browser in a win form application and I am experiencing issues when opening a URL.
The URL I pass in as a new URL instance is encoded with:
/ as %2f , ? as %3f and the
= as %3d
But when I debug my code I can see that the absolute URL or any of the other ones in the webbrowser.url.* is decoded as / , ? and =.
How do I keep the URL encoded? The URL will not work if It is not encoded like that.
I found a solution to my problem, when you have a URL that looks something like this:
domain.com/action/doaction/?identity=12354698789
And you want it encoded like this:
domain.com/logon?returnurl=action%2fdoaction%2f%3fidentity%3d12354698789
That does not work in your web browser. It decodes it to the first url.
I needed the id in the doaction controller so I used this code:
string orgId = ControllerContext.RouteData.Values["id"].ToString();
It returns that url, if unsure, debug and trace through, you will find the right key and value.
Why is it a problem?
If you want the undecoded URL, use the HttpRequest.RawUrl Property. The query string is automatically decoded by default and there is no public parameter that would turn it off.

301 Redirect with unicode characters - C#

I need to do a 301 redirect on a URL that may have Unicode characters in it.
HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)
Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.
You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)
So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.
I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.
That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx
which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.
After I got the Uri working with unicode, I pieced together the url and did a redirect:
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", uri.Scheme + "://" + uri.DnsSafeHost + uri.PathAndQuery + uri.Fragment);
Response.End();
This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.

Categories