Android的:无法向防火墙后面的HTTP请求
-
23-09-2019 - |
题
在标准getUrlContent工作welll当不存在防火墙。但我有例外,当我尝试做一个防火墙后面。
我试图设置“HTTP代理服务器”中AVD经理,但没有奏效。任何想法如何正确设置它?
和BTW:从Android文档“你可以使用-verbose代理选项来诊断代理连接问题” -verbose代理是不是在所有有效的选项。
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);
}
}
解决方案
看看这个小野兽会帮助你。这可能是因为你需要这个正在运行的模拟器图像英寸
http://openhandsetmagazine.com/一十一分之二千零七/提示-HOWTO-CONNECT-机器人仿真器隐藏代理/
其他提示
您可以在代码中设置代理了。
public void setProxy(DefaultHttpClient httpclient) {
final String PROXY_IP = "<insert your IP here>";
final int PROXY_PORT = <insert_PROXY_PORT#>;
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(PROXY_IP, PROXY_PORT),
new UsernamePasswordCredentials(
"username", "password"));
HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
}
不隶属于 StackOverflow