The objective:
Make a console app that send a simple get request - in exactly the right way.
The problem:
I have a url like this:
http://myhost/somepage.do?Search01=コード番号=123456&Search02=改訂番号=2
When I copy and past this into IE 11 or lower, the page I want returns correctly. When I copy and past this into IE Edge, chrome, firefox, it returns an error saying it does not properly recognize the parameters.
I investigated this with Wireshark, and I can see quite clearly that ol' IE is sending the url with some kind of different encoding:
Whereas Chrome does a more expected encoding:
I don't fully understand what is happening here, but it seems that this server I am sending the message to, is somewhat TAILORED towards ol' IE's messed up way of encoding the url - because it is only replying to the messed up requests.
I have checked other things like the useragent etc - it makes no difference. This server is running a service which is very old (maybe using ASP).
So, my objective is to emulate this messed up encoding in a console app. How do I do it?
So, with some help from understanding what might be happening via: This stackoverflow question
I came to realize how my url is being encoded.
My computer is Japanese, so the default codepage is 932. After much messing around with a sample console app, and watching the packets in Wireshark, I realized that no matter what I did, the default
HttpClient
andWebClient
will always UrlEncode my URL correctly regardless of what encoding I used. This is not how ol' IE encodes it's URLs.I dug deeper and found that in the source for
HttpClient
(andWebClient
) it uses the classUri
, which has a constructor with parameter :DontEscape
which I thought, "Eurika!" but it turns out this constructor is Obsolete, and there is no way to not make URL's automatically escape themselves when usingHttpClient
orWebClient
.So I had to use
TcpClient
and make my own request instead. Which I stole from here:The key part of this code is:
var header = Encoding.GetEncoding(932).GetBytes(builder.ToString());
This forces the string to be encoded in my codepage, which required that the codepage provider was registered, so at the top:
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
The usage is simple:
await HttpRequestAsync("123.456.789.123", "/somepage.do?Search01=コード番号=123456&Search02=改訂番号=2");