The HttpWebRequest GetRequestStream in C # is inefficient, and even occasionally dies

Source: Internet
Author: User

original link: http://www.crifan.com/csharp_httpwebrequest_getrequeststream_dead_hang_low_efficiency/"Resolved" C # HttpWebRequest GetRequestStream execution is too inefficient to even die occasionally

Problem

In C #, the corresponding post-type HTTP request is executed before it is committed:

Stream Postdatastream = req. GetRequestStream ();

Then populate the corresponding post data, and then submit the HTTP request.

However, when debugging, it is found that each execution of GetRequestStream is slow.

Slow also forget, results recently found that, some time, the implementation of req. GetRequestStream (); die.

"Resolution Process"

1. Online search, according to GetRequestStream efficiency why so low? Explanation, said yes. NET automatically searches for proxies every time, so it's slow, and if you don't set up a proxy here, you should copy it directly to null:

Req. Proxy = null;

Then go to execute getrequeststream, so it won't be slow.

And then went to try it as if it were the effect.

2. Results the next day the same code, to debug, the result GetRequestStream place, or dead.

The cause is unknown.

3. Later, the same code, run again, will not die, although it is also sometimes inefficient, slow reaction, but will not die. The specific root cause, the feeling still did not find.

4. Subsequent commissioning, found for:

Req. Proxy = null;

It seems to have no effect.

Just happened to find the original official explanation about setting the proxy to null:

"C # 3.0 In a nutshell, third edition:a Desktop Quick Reference" Chapter 14. Networking:

Warning

If you do not have a proxy, you must set the "proxy" to null on all WebClient and WebRequest objects. Otherwise, the Framework may attempt to "Auto-detect" your proxy settings, adding-to-seconds request. If you ' re wondering why your Web requests execute slowly, this is probably it!

But here, for what I have encountered, the execution of GetRequestStream will die, still no help.

5. The result of multiple debugs is, if it is for:

Stream Postdatastream = req. GetRequestStream ();

is one-step debugging, it seems that very few will die.

And if run directly, no break point, then often easy to die.

6. Reference: GetRequestStream throws Timeout exception randomly, to try:

using (Stream Postdatastream = req. GetRequestStream ()) {    postdatastream.write (postbytes, 0, postbytes.length);    Postdatastream.close ();}

Will still die.

7. Online See: Httpwebrequest.getrequeststream () hangs, it seems that this problem, it seems to have been officially admitted a bug.

But even if it's a bug, it's explained in. NET 4.0 Beta2, and I can't use. NET 4.0 here, only with. NET 2.0/3.0/3.5, so if it's a bug, it's really tragic ...

Let's go ahead and see if there are any other solutions.

8. Try the following code:

try{    Stream postdatastream = req. GetRequestStream ();    Postdatastream.write (postbytes, 0, postbytes.length);    Postdatastream.close ();} catch (WebException ex) {    MessageBox.Show (ex. Message);}

Single-Step Debugging:

Stream Postdatastream = req. GetRequestStream ();

After a long time, the discovery of the exception WebException ex, the "Unable to connect to the remote server" error occurred, with the details as follows:

ex{"Unable to connect to the remote server"}system.net.webexceptionbase{"Unable to connect to the remote"}system.in validoperationexception {system.net.webexception}base{"Unable to connect to the remote server"}system.systemexception {system.net.webexception}base{"Unable to connect to the remote server"}system.exception {system.net.webexception}_ ClassNamenullstring_datanullSystem.Collections.IDictionary_dynamicMethodsnullobject_ ExceptionMethodnullSystem.Reflection.MethodBase_exceptionMethodStringnullstring_helpURLnullstring_ hresult-2146233079int_innerexception{"A connection attempt failed because the connected party does not properly respond aft Er a period of time, or established connection failed because connected host have failed to respond 65.55.254.42:443 "}syste m.exception {system.net.sockets.socketexception}_message "Unable to connect to the remote server" String_ Remotestackindex0int_remotestacktracestringnullstring_sourcenullstring_stacktrace{sbyte[96]}object {sbyte[]}_ StacktraCestringnullstring_xcode-532459699int_xptrs0system.intptrdata{system.collections.listdictionaryinternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} helplinknullstringhresult-2146233079intinnerexception{"A connection attempt failed because the connected party does not Properly respond after a period of time, or established connection failed because connected host have failed to respond 65. 55.254.42:443 "}system.exception {system.net.sockets.socketexception}istransientfalseboolmessage" Unable to connect To the remote server "Stringsource" System "Stringstacktrace" at System.Net.HttpWebRequest.GetRequestStream ( transportcontext& context) \ r \ n at System.Net.HttpWebRequest.GetRequestStream () \ r \ n at InsertSkydriveFiles.skydrive.beforeUploadFile () "Stringtargetsite{system.io.stream GetRequestStream ( System.Net.TransportContext ByRef)}system.reflection.methodbase {system.reflection.runtimemethodinfo}static MembersInternalStatusServicePointFatalSystem.Net.WebExceptiOnInternalStatusm_InternalStatusServicePointFatalSystem.Net.WebExceptionInternalStatusm_ ResponsenullSystem.Net.WebResponsem_ StatusConnectFailureSystem.Net.WebExceptionStatusResponsenullSystem.Net.WebResponseStatusConnectFailureSystem.Net.WebExce Ptionstatus

The thing that I find very incomprehensible here is that I just call GetRequestStream, get the stream of request, not from the server side of the response, why also go to link remote server, instead of calling a library function locally, Get the corresponding stream???

"PostScript 2012-03-01"

Later found, as if this problem, and GetResponse occasionally died, is the same reason, and then also solved the problem, the specific process and methods, please refer to:

GetResponse or GetRequestStream of "resolved" HttpWebRequest occasionally timed out + summary of possible and corresponding workarounds for various timeout deaths

The HttpWebRequest GetRequestStream in C # is inefficient, and even occasionally dies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.