1

I am trying to insert data via web service. The code below writes to the database; however, I have an error (see bottom). What goes wrong here? and how to fix it?

//Create the web request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

//Set type to POST
request.Method = "POST";
request.ContentType = "text/XML";

// Write data  
using (StreamWriter postStream = new StreamWriter(request.GetRequestStream()))
{
     postStream.WriteLine("<biz_in><phone_no>+1604333333</phone_no></biz_in>");
     postStream.Dispose();
}

Error:

System.Net.WebException was unhandled 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 System.IO.StreamWriter.Dispose(Boolean disposing) at System.IO.StreamWriter.Close() at ConsoleApplication1.Program.Main(String[] args) in C:/Program Files/Program.cs:line 62 at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

tvanfosson
  • 524,688
  • 99
  • 697
  • 795

4 Answers4

1

You might want to increase the timeOut of the request.

I had the same issue where I was trying to upload 26Mb file in chunks, and it was failing with same error. I increased the timeout and it successfully posted it. (I was dealing with Amazon).

Sameers Javed
  • 342
  • 2
  • 5
  • 16
1

StreamWriter closes the underlying stream when you dispose it.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Streamwriter proves to be much trouble than it should be. I've chosen to use just "Stream" instead as such Stream postData = request.GetRequestStream(); postData.Write(byteData, 0, byteData.Length); postData.Close(); However, I found another problem. The code above runs well in Console application. Since this is intended to be a mobile win 5 application, execution on my mobile ph gives another error. Error: An error message cannot be displayed because an optional resource What do you recommend? assembly containing it cannot be found –  Jun 22 '09 at 06:22
  • Sry, error msg should be: An error message cannot be displayed because an optional resource assembly containing it cannot be found –  Jun 22 '09 at 06:23
0

Include this:

request.KeepAlive = false;

This will work, but will probably imply a performance penalty as there won't be any re-use (also known as HTTP pipelining) of TCP connections. TCP connections are now closed immediately and reopened on each HTTP-request.

SuPra
  • 8,488
  • 4
  • 37
  • 30
0

If you have been instructed to set the MIME type to text/XML; I would presume your server is expecting valid XML to be sent in the body of the message.

The content you have in your example, namely this line:

postStream.WriteLine("+1604333333");

... will not qualify as valid XML data.

Try sending a valid XML document and see what happens from there. Here is a minimal document you can send. You should receive a different error as a result of sending this.

postStream.WriteLine("<?xml version=\"1.0\"?><test />");
meklarian
  • 6,595
  • 1
  • 23
  • 49