安卓:无法向HTT prequest防火墙后面 [英] Android: Unable to make httprequest behind firewall

查看:115
本文介绍了安卓:无法向HTT prequest防火墙后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准getUrlContent工作welll时,有没有防火墙。但我得到了异常,当我尝试做一个防火墙后面。

我试着设置AVD管理HTTP代理服务器,但没有奏效。不知道如何正确设置它?

和BTW:从Android电子文档,您可以使用-verbose-proxy选项来诊断代理连接问题 -verbose代理是不是一个有效的选项都没有。

 受保护的静态同步字符串getUrlContent(字符串URL)抛出ApiException {
    如果(url.equals(试一试)){
        返回感谢;

    }
    如果(sUserAgent == NULL){
        抛出新ApiException(用户代理字符串必须是prepared);
    }

    //创建客户端,并设置了特定的用户代理字符串
    HttpClient的客户端=新DefaultHttpClient();
    HTTPGET请求=新HTTPGET(URL);
    request.setHeader(用户代理,sUserAgent);

    尝试 {
        HTT presponse响应= client.execute(要求);

        //检查服务器的响应是否有效
        状态行状态= response.getStatusLine();
        如果(status.getStatus code()!= HTTP_STATUS_OK){
            抛出新ApiException(从服务器无效的回应:+
                    status.toString());
        }

        //从拉动响应内容流
        HttpEntity实体= response.getEntity();
        InputStream中的InputStream = entity.getContent();

        ByteArrayOutputStream内容=新ByteArrayOutputStream();

        //读取响应转换成一个缓冲流
        INT的ReadBytes = 0;
        而((的ReadBytes = inputStream.read(sBuffer))!= -1){
            content.write(sBuffer,0,的ReadBytes);
        }

        //从缓冲流返回结果
        返回新的String(content.toByteArray());
    }赶上(IOException异常E){
        抛出新ApiException(问题与API沟通,E);
    }
}
 

解决方案

看看这个小野兽会帮助你。这可能是你需要这个在正在运行的仿真器的图像。

http://openhandsetmagazine.com/2007/11/tips-howto-connect-android-emulator-behind-proxy/

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?

and btw: from android documentation "You can use the -verbose-proxy option to diagnose proxy connection problems." -verbose-proxy is not a valid option at all.

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);
    }
}

解决方案

See if this little beastie will help you. It may be that you need this in the emulator image you are running.

http://openhandsetmagazine.com/2007/11/tips-howto-connect-android-emulator-behind-proxy/

这篇关于安卓:无法向HTT prequest防火墙后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆