J
Jayme Pechan
I'm trying to do a simple web post to a web server with C# and am having
lots of problems. Here is the code:
try
{
XmlDocument docRequest = new XmlDocument();
XmlNode nodePush =
docRequest.AppendChild(docRequest.CreateElement("Push"));
nodePush.Attributes.Append(docRequest.CreateAttribute("type")).Value =
"subscribe";
XmlNode nodeGo = nodePush.AppendChild(docRequest.CreateElement("go"));
nodeGo.Attributes.Append(docRequest.CreateAttribute("href")).Value =
http://192.168.55.47/wml/resubscribe.xml;
nodeGo.Attributes.Append(docRequest.CreateAttribute("method")).Value =
"get";
string url = http://192.168.55.36/forms/push;
byte[] postbytes = Encoding.ASCII.GetBytes("XMLData=" +
HttpUtility.UrlEncode(docRequest.OuterXml));
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
r.Method = "POST";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = postbytes.Length;
Stream ws = r.GetRequestStream(); // exception here!
ws.Write(postbytes, 0, postbytes.Length);
ws.Close();
WebResponse wr = r.GetResponse();
string responseData = "";
using (StreamReader tr = new StreamReader(wr.GetResponseStream()))
{
responseData = tr.ReadToEnd();
tr.Close();
Debug.WriteLine("Response=" + responseData);
}
wr.Close();
}
catch (Exception except)
{
Debug.WriteLine("Exception in ReRegisterPhone: " + except.Message);
}
The message I get is:
The underlying connection was closed: The connection was closed
unexpectedly.
I wrote a JScript to do the same thing and it works fine...
var Req = new ActiveXObject("Msxml2.ServerXMLHTTP");
Req.open("POST", "http://192.168.55.36/forms/push", false);
var objXML = new ActiveXObject("Msxml2.DOMDocument");
objXML.async = false;
var rootElem = objXML.createElement("Push");
objXML.appendChild(rootElem);
var newattrib;
newattrib = objXML.createAttribute("type");
newattrib.value = "subscribe";
rootElem.setAttributeNode(newattrib);
var goElem = objXML.createElement("go");
rootElem.appendChild(goElem);
newattrib = objXML.createAttribute("href");
newattrib.value = "http://192.168.55.47/wml/resubscribe.xml";
goElem.setAttributeNode(newattrib);
newattrib = objXML.createAttribute("method");
newattrib.value = "get";
goElem.setAttributeNode(newattrib);
Req.send("XMLData=" + encodeURI(objXML.xml));
WScript.echo( Req.responseText );
WScript.Sleep(50000);
Can anyone see what I'm missing here? Strangely, if I don't set the
ContentLength, the exception does not occur at r.GetRequestStream but waits
until GetResponse
Any ideas would be appreciated.
Jayme
lots of problems. Here is the code:
try
{
XmlDocument docRequest = new XmlDocument();
XmlNode nodePush =
docRequest.AppendChild(docRequest.CreateElement("Push"));
nodePush.Attributes.Append(docRequest.CreateAttribute("type")).Value =
"subscribe";
XmlNode nodeGo = nodePush.AppendChild(docRequest.CreateElement("go"));
nodeGo.Attributes.Append(docRequest.CreateAttribute("href")).Value =
http://192.168.55.47/wml/resubscribe.xml;
nodeGo.Attributes.Append(docRequest.CreateAttribute("method")).Value =
"get";
string url = http://192.168.55.36/forms/push;
byte[] postbytes = Encoding.ASCII.GetBytes("XMLData=" +
HttpUtility.UrlEncode(docRequest.OuterXml));
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
r.Method = "POST";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = postbytes.Length;
Stream ws = r.GetRequestStream(); // exception here!
ws.Write(postbytes, 0, postbytes.Length);
ws.Close();
WebResponse wr = r.GetResponse();
string responseData = "";
using (StreamReader tr = new StreamReader(wr.GetResponseStream()))
{
responseData = tr.ReadToEnd();
tr.Close();
Debug.WriteLine("Response=" + responseData);
}
wr.Close();
}
catch (Exception except)
{
Debug.WriteLine("Exception in ReRegisterPhone: " + except.Message);
}
The message I get is:
The underlying connection was closed: The connection was closed
unexpectedly.
I wrote a JScript to do the same thing and it works fine...
var Req = new ActiveXObject("Msxml2.ServerXMLHTTP");
Req.open("POST", "http://192.168.55.36/forms/push", false);
var objXML = new ActiveXObject("Msxml2.DOMDocument");
objXML.async = false;
var rootElem = objXML.createElement("Push");
objXML.appendChild(rootElem);
var newattrib;
newattrib = objXML.createAttribute("type");
newattrib.value = "subscribe";
rootElem.setAttributeNode(newattrib);
var goElem = objXML.createElement("go");
rootElem.appendChild(goElem);
newattrib = objXML.createAttribute("href");
newattrib.value = "http://192.168.55.47/wml/resubscribe.xml";
goElem.setAttributeNode(newattrib);
newattrib = objXML.createAttribute("method");
newattrib.value = "get";
goElem.setAttributeNode(newattrib);
Req.send("XMLData=" + encodeURI(objXML.xml));
WScript.echo( Req.responseText );
WScript.Sleep(50000);
Can anyone see what I'm missing here? Strangely, if I don't set the
ContentLength, the exception does not occur at r.GetRequestStream but waits
until GetResponse
Any ideas would be appreciated.
Jayme