开发者

POST request using sockets C#

I'm trying to make an auction sniper for a site. To place a bid you need to send 4 parameters(and cookies of course) to /auction/place_bid. I need to use sockets, not HttpWebRequest. Here's the 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 = "";
        do
        {
            bytes = socket.Receive(dataReceived, dataReceived.Length, 0);
            page = page + Encoding.ASCII.GetString(dataReceived, 0, bytes);
        }
        while (bytes > 0);

        return page;

When I'm trying to receive the webpage Visual Studio says that "Operation on an unblocked socket cannot be completed immediatly", when I add

socket.Blocking = true;

My application stops responsing and after ~1 minute it returns page, but it's empty! When I'm trying to make a GET request it works perfect. I hope you will help me. By the way, this is the first time when I use sockets so my code is pretty bad, sorry about that.

*I'm using a ConnectSocket class, which was given as an example at msdn (The link leads开发者_如何学Python to Russian MSDN, sorry, I didn't find the same article in English, but you'll understand the code anyway)


The Content-Length header should indicate the size of the content. You're setting it to the total size of your headers and content.

Encoding.UTF8.GetByteCount(request1+request3)+23).ToString()

Since the content part of your message is just request3, the server is patiently waiting for ByteCount(request1)+23 more bytes of content which you never send.

Try this instead:

"Content-Length: " + Encoding.UTF8.GetByteCount(request3).ToString() + "\r\n"

Another issue looks like your 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 always return immediately whether or not they've completed yet, you need a loop that keeps calling Receive() until the operation has actually completed. Here, if the call to Receive() returns 0 (which it almost certainly will the first time) you exit the loop.

You should at least change it to while (bytes <= 0) which would get you at least some data (probably just the first packet's worth or so). Ideally, you should keep calling Receive() until you see the Content-Length header in the reply, then continue calling Receive() until the end of the headers, then read Content-Length more bytes.

Since you're using sockets, you really have to re-implement the HTTP protocol.


As people already has pointed out: HttpWebRequest is not the cause of you performance issues. Switching to a socket implementation will not affect anything.

The fact is that the HttpWebRequest can do zillions of stupid things if it want to, and it will still be faster than the time it takes to get stuff from the webserver.

Switching to a socket implementation might speed things up if you have good knowledge when it comes to sockets AND the http protocol. You clearly do not have that, so I would recommend that you go back to HttpWebRequest again.

You might want to use WebClient if you are going to fetch lots of pages from the same webserver since it will keep the connection alive.

Update

I don't need a lot of connections, I need to make 1 request a time, and it should be as fast as it possible

Well. Then it doesn't really matter which implementation you use. The network latency will ALWAYS be a lot larger than the actual HTTP client implementation. Building a HTTP request doesn't take very much resources, parsing a response doesn't do that either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜