Trying to send an HTTP request over sockets

2019-08-03 02:44发布

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?

5条回答
走好不送
2楼-- · 2019-08-03 03:17

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.

查看更多
不美不萌又怎样
3楼-- · 2019-08-03 03:18

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

查看更多
Juvenile、少年°
4楼-- · 2019-08-03 03:30

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

查看更多
放荡不羁爱自由
5楼-- · 2019-08-03 03:31

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.

查看更多
走好不送
6楼-- · 2019-08-03 03:41

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.

查看更多
登录 后发表回答