NameResolutionFailure in Mono but not .NET

2019-04-28 14:43发布

I am making HTTPS POST requests (same problem with HTTP) using C#

byte[] byteArray = Encoding.UTF8.GetBytes("var1=blah&var2=hah");
HttpWebRequest request = (HttpWebRequest)(WebRequest.Create("https://www.example.com"));
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();

The preceding code works great in both .NET and Mono when I don't have to go through a proxy. When I have to use a proxy, then it works when run on .NET but in Mono fails with the following

WebException: Error: NameResolutionFailure
    at System.Net.HttpWebRequest.EndGetRequestStream (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
    at System.Net.HttpWebRequest.GetRequestStream () [0x00000] in <filename unknown>:0

Also, running in a browser with the same proxy configuration works fine. Any reason why Mono would throw a NameResolutionFailure while .NET does not?

There was a similar stackoverflow question that had a work around of using the direct ip when creating the request and then adding the domain to the request.Host. However, the proxy I have to go through rejects this kind of request. Help!

Operating System is Windows 7, Mono version is 2.6.5

标签: c# .net mono
1条回答
迷人小祖宗
2楼-- · 2019-04-28 15:28

Hint: First of all check your web browse in your phone. If it will be the same situation - then you have to worry about. I had the same situation and it happened all time when EMULATOR doesn't have permission to net. Try on normal device.

查看更多
登录 后发表回答