Click here to Skip to main content
15,175,962 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
code:
C#
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();
Posted
Updated 10-Dec-19 0:40am
v4
Comments
Dalek Dave 23-Feb-11 6:36am
   
Edited for Code Block.

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.
   
Comments
Punit Belani 23-Feb-11 6:09am
   
ok now i am 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();
J a a n s 23-Feb-11 6:18am
   
Try assigning Request.ContentLength as PostData.Length (no need to increase it by 1000)
Punit Belani 23-Feb-11 6:23am
   
than it gives me "The remote server returned an error: (500) Internal Server Error"
J a a n s 23-Feb-11 6:28am
   
In that case, your target website is not able to process the request properly. Have you created this website? If yes try to debug it and find the issue with the server.
Punit Belani 23-Feb-11 6:32am
   
when i am manually copy the string of PostData to url tab of browser it gives response fine.. but not via code. why???
please help.. i am stucked..
J a a n s 23-Feb-11 6:36am
   
manually copying? Then your page may be expecting a GET request, ie querystrings.
Punit Belani 23-Feb-11 6:44am
   
when am trying with GET Request it gives me something different error. "You can not use this type of verb"
J a a n s 23-Feb-11 7:32am
   
While using GET method, you should not get the request stream. You have to pass the whole details as url itself (like www.abc.com/abc.aspx?id1=val1 ). For more details pls refer the article http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx
Dalek Dave 23-Feb-11 6:36am
   
Sage Advice.
   
Comments
Espen Harlinn 26-Feb-11 10:18am
   
Nice additions, my 5
Just disable HTTP, Keep Alive for posting the Requests.
   
Comments
Punit Belani 23-Feb-11 5:56am
   
Now it gives me error:
The remote server returned an error: (500) Internal Server Error
[no name] 23-Feb-11 5:59am
   
Hope http://forums.iis.net/t/1152487.aspx link might help you to solve your issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900