Web Post problems

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
 
J

Jayme Pechan

I discovered that the C# version adds an extra blank line between the header
and the data. This causes a problem for the web server in question. Does
anyone know of a way to remove this extra line?

------------ JScript ------------
POST /forms/push HTTP/1.1
Accept-Language: en-us
Content-Length: 140
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: localhost:5000
Connection: Keep-Alive

XMLData=%3CPush%20type=%22subscribe%22%3E%3Cgo%20href=%22http://192.168.55.47/wml/resubscribe.xml%22%20method=%22get%22/%3E%3C/Push%3E%0D%0A

------------ C# ------------
POST /forms/push HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: localhost:5000
Content-Length: 149
Expect: 100-continue
Connection: Keep-Alive


XMLData=%3cPush+type%3d%22subscribe%22%3e%3cgo+href%3d%22http%3a%2f%2f192.168.55.47%2fwml%2fresubscribe.xml%22+method%3d%22get%22+%2f%3e%3c%2fPush%3e
-----------------------

I coded it with the TcpClient and it works fine. I'd still prefer to use
the classes if I can somehow get rid of the extra line. Thanks.

Jayme
 
J

Joerg Jooss

Thus wrote Jayme,
I discovered that the C# version adds an extra blank line between the
header and the data. This causes a problem for the web server in
question. Does anyone know of a way to remove this extra line?
[...]

The HTTP 1.1 spec *requires* that blank line (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4).


Any server that doesn't accept this is inherently broken. You'll have to
stick with creating invalid HTTP requests by low-level classes such as Socket
or TcpClient :-(

Cheers,
 
J

Joerg Jooss

Thus wrote Joerg,
Thus wrote Jayme,
I discovered that the C# version adds an extra blank line between the
header and the data. This causes a problem for the web server in
question. Does anyone know of a way to remove this extra line?
[...]

The HTTP 1.1 spec *requires* that blank line (see
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4).

Any server that doesn't accept this is inherently broken. You'll have
to stick with creating invalid HTTP requests by low-level classes such
as Socket or TcpClient :-(

Um, wait. If you've meant that you get *2* CRLFs between headers and body,
it is a bug in the client code, although a server should still be able to
accept such a message.

Cheers,
 
Top