网络发布错误“请求被中止:请求被取消." [英] Web posting error "The request was aborted: The request was canceled."

查看:505
本文介绍了网络发布错误“请求被中止:请求被取消."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

req = System.Net.WebRequest.Create(uri);
        req.Credentials = System.Net.CredentialCache.DefaultCredentials;
        req.Proxy.Credentials = CredentialCache.DefaultCredentials;

        req.Method = "POST";
        req.Timeout = 2139999999;
        req.ContentType = "text/xml";       
        string text = XMLString;//Xml string
        req.ContentLength = text.Length + 1000;
        System.IO.StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream());
        writer.WriteLine(text);
        writer.Close();
        rsp = req.GetResponse();
        System.IO.StreamReader reader = new System.IO.StreamReader(rsp.GetResponseStream());
        string rp = reader.ReadToEnd();
        reader.Close();


向我返回网络异常:


return me web exception:

System.Net.WebException was caught
  Message=The request was aborted: The request was canceled.
  Source=System
  StackTrace:
       at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
       at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
       at System.Net.ConnectStream.Dispose(Boolean disposing)
       at System.IO.Stream.Close()
       at Ping.FormPostType(String uri, String buyerid) in c:\Inetpub\wwwroot\TestIceBlue\Ping.aspx.cs:line 545
       at Ping.FirstTask(String i, String uri, String buyerid, String PostT, String ResponseT) in c:\Inetpub\wwwroot\TestIceBlue\Ping.aspx.cs:line 325
  InnerException: System.IO.IOException
       Message=Cannot close stream until all bytes are written.
       Source=System
       StackTrace:
            at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)


请帮助我如何解决此异常?


我也在尝试下面的代码,但仍然给我错误请求已中止:请求已取消.":


Please help how can I resolve this exception?


I am also trying with below code, but still it gives me error "The request was aborted: The request was canceled.":

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri); Request.Credentials = System.Net.CredentialCache.DefaultCredentials; Request.Proxy.Credentials = CredentialCache.DefaultCredentials; Request.Timeout = 2139999999; 
Request.Method = "POST"; 
Request.ContentType = "application/x-www-form-urlencoded"; 
ASCIIEncoding AscTest = new ASCIIEncoding(); 
Byte[] PostData = AscTest.GetBytes(sQryString); 
Request.ContentLength = PostData.Length + 1000; 
Request.KeepAlive = false; 
System.IO.Stream RequestStream = Request.GetRequestStream(); RequestStream.Write(PostData, 0, PostData.Length); 
RequestStream.Close(); 
System.IO.StreamReader Reader = new StreamReader(Request.GetResponse().GetResponseStream()); 
String ResultHTML = Reader.ReadToEnd();

推荐答案

似乎将数据写入请求流是不正确的.请尝试以下步骤.

1. ContentType 可以是"application/x-www-form-urlencoded"
2.将XMLString 转换为字节数组.
3. ContentLength 应该是数组的长度.
4.写字节数组而不是字符串.
It seems that writing the data to the request stream is not proper. Try the following steps.

1. ContentType can be ''application/x-www-form-urlencoded''
2. Convert the XMLString to a byte array.
3. ContentLength should be length of the array.
4. Write the byte array instead of the string.


您有任何解决方案吗?(我的意思是,在检查完其他答案之后),如果没有,请看一下这些...

对已取消的Web请求"进行问题排查 [ .NET,HttpWebRequest.KeepAlive和ServicePoint.ConnectionLimit [ .NET 2.0同步Web服务调用给出请求被中止:请求被取消."错误 [^ ]
Did you get any solutions?(I mean after checked the other answers) if not, have a look at these...

Troubleshooting "Canceled Web Request" [^]

.NET, HttpWebRequest.KeepAlive And ServicePoint.ConnectionLimit [^]

.NET 2.0 Simultaneous Web Service Calls give "The request was aborted: The request was canceled." error[^]


只需禁用HTTP,保持活动状态即可发布请求.
Just disable HTTP, Keep Alive for posting the Requests.


这篇关于网络发布错误“请求被中止:请求被取消."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆