views:

1840

answers:

6

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?

A: 

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

Brian
A: 

Thanks for the suggestion Brian. No luck though. I changed my onConnect function to:

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

No difference.

Mike Keen
I'm referring to a single-byte encoding. Multi-byte will lead to a similar issue.
Brian
+9  A: 

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.

Graeme Perrow
+1  A: 

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.

Mike Keen
A: 

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.

Mitch Haile
A: 

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

aleks_raiden