Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
alxwang
Posts: 8
Registered: ‎10-10-2008
My Device: Not Specified
Accepted Solution

How to make HTTP GET request with socket?

I want to use socket to do HTTP GET but I always get 501 error back.

 

Result:

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
<p> to /index.html not supported.<br />
</p>
<hr>
<address>Apache/2.0.52 (CentOS) Server at li43-231.members.linode.com Port 80</address>
</body></html>

 

Code:

streamConnection =
            (StreamConnection) Connector.open(getUrl(3)+APN_STR,Connector.READ_WRITE);
           
            // create DataOuputStream on top of the socket connection
            outputStream = streamConnection.openOutputStream();
            dataOutputStream = new DataOutputStream(outputStream);
           
            // send the HTTP request
            dataOutputStream.writeChars("GET /index.html http/2.0\n");
            dataOutputStream.writeChars("host: badev.liveblockauctions.com\n");
            dataOutputStream.writeChars("User-Agent:BlackBerry/3.2.1\n");
            dataOutputStream.writeChars("Content-Language:en-US\n\n");

            dataOutputStream.flush();
           
            // create DataInputStream on top of the socket connection
            inputStream = streamConnection.openInputStream();
            dataInputStream = new DataInputStream(inputStream);
           
            // retrieve the contents of the requested page from Web server
            int inputChar;
            int size=0;
            StringBuffer results=new StringBuffer();
            while ( (inputChar = dataInputStream.read()) != -1) {
                size++;
                results.append((char) inputChar);
            }

 

JDE 4.6 and blackberry bold.

 

Any suggestion?

 

Thanks a lot!

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: How to make HTTP GET request with socket?

see many of my earlier posts with links to ietf and suggestions on setting up dummy servers with known working

requestors- I use wget with cygwin or IE and a perl server socket. I think the get needs something about http1.x or something

but I'd have to look,

 

$ perlform -serve 80
GET /something.dum HTTP/1.0
User-Agent: Wget/1.9.1
Host: 127.0.0.1
Accept: */*
Connection: Keep-Alive

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make HTTP GET request with socket?

[ Edited ]

Here is some code that I use to do something like what you want.  I suspect that if looked you at the bytes that you send as a result of your writeChars(), you will find it doesn't match the http spec.  For example, I think you need CRLF, whereas I think you are just sending LF at the end of each line.  However I'm not an http expert, I just got this going for something I was working on.  Hopefully you can use it to get your code working.

 

Note that the URL I use is _target + ":80", _requestString is just the GET request String and RegisterCF.getEmailAddress(), just gets this device's email address.

 

c = (StreamConnection)Connector.open(completeURL); is = c.openInputStream(); os = c.openOutputStream(); String sendString = "GET " + _requestString + " HTTP/1.0 \r\n" + "Host: " + _target + "\r\n" + "From: " + RegisterCF.getOurEmailAddress() + "\r\n" + "\r\n"; os.write(sendString.getBytes()); os.close();

 

 

 

 

 

Message Edited by peter_strange on 10-17-2008 06:31 PM
Please use plain text.