locked
HttpWebRequest does not post any data

    Question

  • Hi, so this is my very werid problem.... HttpWebRequest does not send post data to remove website. I have no idea whats wrong. I tried several websites, several code snippets written by other people too. Those performed just as my own code. For the sake of simplicity ill paste someone others code snippet that is way more simple than my one and still does not send post data, it should however.
    			HttpWebRequest HttpWReq =(HttpWebRequest)WebRequest.Create("http://google.com/");
    
    			ASCIIEncoding encoding=new ASCIIEncoding();
    			string pd = "username=foo";
    			pd += ( "&password=boo");
    			byte[] d = encoding.GetBytes(pd);
    
    			HttpWReq.Method = "POST";
    			HttpWReq.ContentType = "application/x-www-form-urlencoded";
    			HttpWReq.ContentLength = d.Length;
    
    			Stream newStream = HttpWReq.GetRequestStream();
    			newStream.Write(d,0,d.Length);
    			newStream.Close();
    This is what request is sent:
    POST / HTTP/1.1
    Content-Type: text/plain
    Host: google.com
    Content-Length: 25
    Expect: 100-continue
    Connection: Keep-Alive
    
    
    Raw packet contents:
     0000: 00 22 b0 4b 22 7f 00 21 5d 15 3b 7c 08 00 45 00 .".K"..!].;|..E.
     0010: 00 a9 27 68 40 00 80 06 95 1f c0 a8 00 65 d1 55 ..'h@........e.U
     0020: ab 64 e5 7c 00 50 bc 99 a4 71 62 ce 39 e4 50 18 .d.|.P...qb.9.P.
     0030: 43 08 bf f2 00 00 50 4f 53 54 20 2f 20 48 54 54 C.....POST / HTT
     0040: 50 2f 31 2e 31 0d 0a 43 6f 6e 74 65 6e 74 2d 54 P/1.1..Content-T
     0050: 79 70 65 3a 20 74 65 78 74 2f 70 6c 61 69 6e 0d ype: text/plain.
     0060: 0a 48 6f 73 74 3a 20 67 6f 6f 67 6c 65 2e 63 6f .Host: google.co
     0070: 6d 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 m..Content-Lengt
     0080: 68 3a 20 32 35 0d 0a 45 78 70 65 63 74 3a 20 31 h: 25..Expect: 1
     0090: 30 30 2d 63 6f 6e 74 69 6e 75 65 0d 0a 43 6f 6e 00-continue..Con
     00a0: 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c nection: Keep-Al
     00b0: 69 76 65 0d 0a 0d 0a 04 a4 e2 54 24 23 9c 14 0d ive.......T$#...
    
    Do you have any idea what could prevent code sending post data?
    Friday, April 03, 2009 9:05 AM

Answers

  • To me, it looks like it *is* sending data.  The request says: Content-Length: 25, so it says it is going to send the data[1].  Presumably there's a following packet containing the data -- which might not be sent until the server has send the intermediary "100 Continue" response.



    [1] For anyone else reading the raw trace, the final nine bytes are not part of the packet.  The IP Header says it is 0x00a9 bytes long, which ends immediately at the end of the CRLFCRLF.
    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, mark the question answered
    • Marked as answer by roxaz Friday, April 03, 2009 10:38 AM
    Friday, April 03, 2009 10:24 AM
  • well there was no other packet with post data.. probably cause google was not supposed to respond to such a weird packet. anyway adding request.KeepAlive = false; and request.ProtocolVersion = HttpVersion.Version10; made it send post data in same packet just like browser does.
    • Marked as answer by roxaz Friday, April 03, 2009 10:38 AM
    Friday, April 03, 2009 10:37 AM

All replies

  • To me, it looks like it *is* sending data.  The request says: Content-Length: 25, so it says it is going to send the data[1].  Presumably there's a following packet containing the data -- which might not be sent until the server has send the intermediary "100 Continue" response.



    [1] For anyone else reading the raw trace, the final nine bytes are not part of the packet.  The IP Header says it is 0x00a9 bytes long, which ends immediately at the end of the CRLFCRLF.
    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, mark the question answered
    • Marked as answer by roxaz Friday, April 03, 2009 10:38 AM
    Friday, April 03, 2009 10:24 AM
  • well there was no other packet with post data.. probably cause google was not supposed to respond to such a weird packet. anyway adding request.KeepAlive = false; and request.ProtocolVersion = HttpVersion.Version10; made it send post data in same packet just like browser does.
    • Marked as answer by roxaz Friday, April 03, 2009 10:38 AM
    Friday, April 03, 2009 10:37 AM