Trying to send an HTTP request over sockets

2019-08-03 02:49发布

问题:

I'm having trouble sending out a simple HTTP request using Actionscript 3's Socket() object. My onConnect listener is below:

function sConnect(e:Event):void {
    trace('connected');
    s.writeUTFBytes('GET /outernet/client/rss/reddit-feeds HTTP/1.1\r\n');
    s.writeUTFBytes('Host: 208.43.71.50:8080\r\n');
    s.writeUTFBytes('Connection: Keep-alive\r\n');
    s.flush();
}

Using a packet sniffer, I can see the request does indeed get sent to the server, but the packet sniffer doesn't identify the protocol as HTTP like it does with other HTTP services. When I run this, the server just eventually disconnects me. I have tried to connect to other simple Apache Servers and just get a malformed request error.

What am I missing here?

回答1:

You have to write another "\r\n" to the stream before the flush to tell the HTTP server that you're finished sending the headers.



回答2:

Turns out I wasn't sending a blank line to the HTTP server after my request. The following minor tweak from the original works:

function sConnect(e:Event):void {
    trace('connected');
    s.writeUTFBytes('GET /outernet/client/rss/reddit-feeds HTTP/1.1\r\n');
    s.writeUTFBytes('Host: 208.43.71.50:8080\r\n');
    s.writeUTFBytes('Connection: Keep-alive\r\n\r\n');
    s.flush();
}

Note the extra \r\n after the last writeUTFBytes. Thanks anyway Brian.

Edit: Thanks Graeme.



回答3:

Instead of using UTF, try with ANSI/ASCII. The encoding may be the cause of the issue.



回答4:

I just wasted a lot of time tracking down what is apparently a serious bug in some combinations of Flash 10 and Linux with writeMultiByte(). I would be very dubious about using writeMultiByte().

I hope this helps you.



回答5:

Maybe wrong, 'ascii' encoding not supported (see http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/charset-codes.html) - use 'us-ascii'