By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,158 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,158 IT Pros & Developers. It's quick & easy.

Web Post problems

P: n/a
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("P ush"));
nodePush.Attributes.Append(docRequest.CreateAttrib ute("type")).Value =
"subscribe";
XmlNode nodeGo = nodePush.AppendChild(docRequest.CreateElement("go" ));
nodeGo.Attributes.Append(docRequest.CreateAttribut e("href")).Value =
http://192.168.55.47/wml/resubscribe.xml;
nodeGo.Attributes.Append(docRequest.CreateAttribut e("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
Feb 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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%20h ref=%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+hre f%3d%22http%3a%2f%2f192.168.55.47%2fwml%2fresubscr ibe.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
Feb 6 '07 #2

P: n/a
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/...ec4.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,
--
Joerg Jooss
ne********@joergjooss.de
Feb 6 '07 #3

P: n/a
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/...ec4.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,
--
Joerg Jooss
ne********@joergjooss.de
Feb 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.