POST request using sockets C# -
i'm trying make auction sniper site. place bid need send 4 parameters(and cookies of course) /auction/place_bid. need use sockets, not httpwebrequest. here's code:
string request1 = "post /auction/place_bid http/1.1\r\nhost: *host here*\r\nconnection: keep-alive\r\nuser-agent: mozilla/4.0 (compatible; msie 6.0; windows nt 5.2; .net clr 1.0.3705;)\r\naccept: /*\r\ncontent-type: application/x-www-form-urlencoded; charset=utf-8\r\nx-requested-with: xmlhttprequest\r\n" + cookies +"\r\n"; string request3 = "token=" + token + "&aid=" + aid + "&bidreq=" + ptzreq + "&recaptcha_challenge_field=" + rcf + "&recaptcha_response_field=" + rrf+"\r\n\r\n"; string request2 = "content-length: " + (encoding.utf8.getbytecount(request1+request3)+23).tostring() + "\r\n"; byte[] datasent = encoding.utf8.getbytes(request1+request2+request3); byte[] datareceived = new byte[10000]; socket socket = connectsocket(server, 80); if (socket == null) { return null; } socket.send(datasent, datasent.length, 0); int bytes = 0; string page = ""; { bytes = socket.receive(datareceived, datareceived.length, 0); page = page + encoding.ascii.getstring(datareceived, 0, bytes); } while (bytes > 0); return page;
when i'm trying receive webpage visual studio says "operation on unblocked socket cannot completed immediatly", when add
socket.blocking = true;
my application stops responsing , after ~1 minute returns page, it's empty! when i'm trying make request works perfect. hope me. way, first time when use sockets code pretty bad, sorry that.
*i'm using connectsocket class, given example @ msdn (the link leads russian msdn, sorry, didn't find same article in english, you'll understand code anyway)
the content-length
header should indicate size of content. you're setting total size of headers and content.
encoding.utf8.getbytecount(request1+request3)+23).tostring()
since content part of message request3
, server patiently waiting bytecount(request1)+23
more bytes of content never send.
try instead:
"content-length: " + encoding.utf8.getbytecount(request3).tostring() + "\r\n"
another issue looks loop:
do { bytes = socket.receive(datareceived, datareceived.length, 0); page = page + encoding.ascii.getstring(datareceived, 0, bytes); } while (bytes > 0);
since non-blocking socket operations return whether or not they've completed yet, need loop keeps calling receive()
until operation has completed. here, if call receive()
returns 0
(which first time) exit loop.
you should @ least change while (bytes <= 0)
@ least data (probably first packet's worth or so). ideally, should keep calling receive()
until see content-length
header in reply, continue calling receive()
until end of headers, read content-length
more bytes.
since you're using sockets, have re-implement http protocol.
Comments
Post a Comment