mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2010
Posts: 9
I
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
I
Joined: Sep 2010
Posts: 9
I'm having trouble with sockets (go figure, lol.)

Here is the code I have so far:

alias bf_sock {
sockopen bfsock http://api.bfbcs.com 80
}

on *:sockopen:bfsock: {
echo -s Socket Opened Successfully.
sockwrite -n bfsock GET /api/360?players=IllogicTC&fields=all HTTP/1.1
sockwrite -n bfsock Host: http://api.bfbcs.com $crlf $+ $crlf
}

on *:sockread:bfsock: {
if ($sockerr) {
echo -s Socket Error. Halting...
HALT
}
echo -s Socket Read Successfully.
var %bfsocktxt
sockread -f %bfsocktxt
echo -s RESULT: %bfsocktxt
sockclose bfsock
echo -s Socket Closed.
}

The echos are in there just for debug purposes, my goal is to be able to retrieve my BFBC2 stats from bfbcs.com's JSON API, but I keep getting:

RESULT: 400 HTTP/1.1 Bad Request

Where on Earth am I going wrong here!? As for the rest, if I can get the information into a variable I can take care of that easily, but this is my first real attempt on sockets frown

Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Try using HTTP/1.0 rather than HTTP/1.1

Joined: Sep 2010
Posts: 9
I
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
I
Joined: Sep 2010
Posts: 9
Tried, and get the same, except it just says HTTP/1.0 in the Bad Request.

Joined: Oct 2009
Posts: 12
E
Pikka bird
Offline
Pikka bird
E
Joined: Oct 2009
Posts: 12
change your host in the sockopen to

sockwrite -n bfsock Host: api.bfbcs.com $crlf $+ $crlf

most of the returns are going to be too long to echo so you will need to cut down the lines to return info. And don't sockclose so fast in the read.

Joined: Sep 2010
Posts: 9
I
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
I
Joined: Sep 2010
Posts: 9
It read good, thanks! And yes, I'll be switching things around. The big block was the not being able to retrieve data in the first place lol.

Thank you!

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
For future reference, you're not supposed to supply the http:// or www. in either the /sockopen command of the Host: section when you write to the socket, just the server name.

Code:
alias bf_sock {
  sockopen bfsock api.bfbcs.com 80
}

on *:sockopen:bfsock: {
  echo -s Socket Opened Successfully.
  sockwrite -n bfsock GET /api/360?players=IllogicTC&fields=all HTTP/1.1
  sockwrite -n bfsock Host: api.bfbcs.com $crlf $+ $crlf
}

on *:sockread:bfsock: {
  if ($sockerr) {
    echo -s Socket Error. Halting...
    HALT
  }
  echo -s Socket Read Successfully.
  var %bfsocktxt
  sockread -f %bfsocktxt
  echo -s RESULT: %bfsocktxt
  sockclose bfsock
  echo -s Socket Closed.
}


Link Copied to Clipboard