Android: Unable to make httprequest behind firewall

Posted by Yang on Stack Overflow See other posts from Stack Overflow or by Yang
Published on 2010-03-29T16:43:18Z Indexed on 2010/03/29 17:13 UTC
Read the original article Hit count: 356

Filed under:
|
|

The standard getUrlContent works welll when there is no firewall. But I got exceptions when I try to do it behind a firewall.

I've tried to set "http proxy server" in AVD manager, but it didn't work. Any idea how to correctly set it up?

protected static synchronized String getUrlContent(String url) throws ApiException {
    if(url.equals("try")){
        return "thanks";

    }
    if (sUserAgent == null) {
        throw new ApiException("User-Agent string must be prepared");
    }

    // Create client and set our specific user-agent string
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    request.setHeader("User-Agent", sUserAgent);

    try {
        HttpResponse response = client.execute(request);

        // Check if server response is valid
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new ApiException("Invalid response from server: " +
                    status.toString());
        }

        // Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        // Read response into a buffered stream
        int readBytes = 0;
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }

        // Return result from buffered stream
        return new String(content.toByteArray());
    } catch (IOException e) {
        throw new ApiException("Problem communicating with API", e);
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about firewall