views:

495

answers:

3

Hi - 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()

A: 

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.

Suvesh Pratapa
A: 

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
+1  A: 

StreamWriter closes the underlying stream when you dispose it.

Joel Coehoorn
Streamwriter proves to be much trouble than it should be. I've chosen to use just "Stream" instead as suchStream 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 resourceWhat do you recommend?assembly containing it cannot be found
Sry, error msg should be:An error message cannot be displayed because an optional resourceassembly containing it cannot be found