简体   繁体   中英

C# HttpWebRequest.GetRequestStream() dies when using Proxy

I have the following code:

        HttpWebRequest request = createRequest("http://somesite", true);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Headers.Add("Accept-Encoding", "gzip, deflate");
        request.Proxy = new WebProxy("what:ever");

        string postData = "my data";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;

        using (var dataStream = request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Flush();
        }

When I ask VS to break on first-chance CLR exceptions, request.GetRequestStream() throws a NullReferenceException internally and then throws a System.Net.WebException (time out) exception to my code.

When I disable the proxy (works directly) it works.

It works just fine in my computer back home but not here (rented apartment, different internet connection).

I'm using the same settings on both machines. Couldn't find anything on it.

Thanks.

Well, changing the implementation to use WebClient.UploadData seems to do the trick. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM