Thiru
Thiru

Reputation: 526

Open HTTP Connection

I need to open an HTTP connection through the wireless internet service on a Blackberry. For example:

(StreamConnection)Connector.open(“socket:// testserver:600;interface=wifi”);

Can anyone provide me with some information on how to do this?

Upvotes: 3

Views: 2460

Answers (2)

Jisson
Jisson

Reputation: 3725

You try this BlackBerry KnowledgeBase article:
What Is - Different ways to make an HTTP or socket connection

Upvotes: 2

gmuhammad
gmuhammad

Reputation: 1438

    public static ResponseBean sendRequestAndReceiveResponse(String method, String absoluteURL, String bodyData, boolean readResponseBody) 
    throws IOException
    {
        ResponseBean responseBean = new ResponseBean();
        HttpConnection httpConnection = null;

        try
        {

            String formattedURL = absoluteURL + "deviceside=true;interface=wifi";
            //String formattedURL = absoluteURL + "deviceside=false"; // If you are using BES
            //String formattedURL = absoluteURL + "deviceside=true"; // If you are using TCP

            httpConnection = (HttpConnection) Connector.open(formattedURL);

            httpConnection.setRequestMethod(method);

            if (bodyData != null && bodyData.length() > 0)
            {                               
                OutputStream os = httpConnection.openOutputStream();
                os.write(bodyData.getBytes("UTF-8"));
            }           

            int responseCode = httpConnection.getResponseCode();
            responseBean.setResponseCode(responseCode);

            if (readResponseBody)
            {
                responseBean.setBodyData(readBodyData(httpConnection));
            }
        }
        catch (IOException ex)
        {                       
            System.out.println("!!!!!!!!!!!!!!! IOException in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
            throw ex;
        }
        catch(Exception ex)
        {                       
            System.out.println("!!!!!!!!!!!!!!! Exception in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
            throw new IOException(ex.toString());
        }
        finally
        {
            if (httpConnection != null)
                httpConnection.close();
        }

        return responseBean;
    }

    public static StringBuffer readBodyData(HttpConnection httpConnection) throws UnsupportedEncodingException, IOException
    {   
        if(httpConnection == null)
            return null;

        StringBuffer bodyData = new StringBuffer(256);                          
        InputStream inputStream = httpConnection.openDataInputStream();

        byte[] data = new byte[256];
        int len = 0;
        int size = 0;

        while ( -1 != (len = inputStream.read(data)) )
        {
            bodyData.append(new String(data, 0, len,"UTF-8"));
            size += len;
        }

        if (inputStream != null)
        {
            inputStream.close();            
        }

        return bodyData;
    }

Upvotes: 0

Related Questions