https连接Android
我有一个应用程序连接到使用Entrust有效证书的Web服务。唯一的区别是它是通配符SSL。https连接Android
的问题是:我得到一个
ERROR/NoHttpResponseException(5195): org.apache.http.NoHttpResponseException: The target server failed to respond
当我在3G上。当在WIFI上它工作,在模拟器上工作,通过我的手机3G工作模拟器。但是3G手机上的应用根本无法工作。在两个不同的网络(Virgin Mobile Canada和Fido)上测试HTC Legend CM7.0.3(2.3.3)和Nexus S 2.3.3。
我有一个PCAP转储从我的设备显示一些错误,但我不明白它真的很好。
确认号码:断开的TCP。确认字段为非零而ACK标志未设置
我也尝试了此question此question的修复。我不知道要去哪里。
顺便说一句,网络服务与3G上的浏览器一起工作。
我们也使用基本身份验证与HttpRequestInterceptor。
我想这是我能给的所有细节。如果需要其他东西,请随时询问。
这个question也有关系,我已经尝试了两个修复,都没有工作。
编辑
我开始认为这可能是更适合serverfault.com
这是转储file和截图
编辑2
这是我用来连接到有问题的Web服务的代码。
protected HttpEntity sendData(List<NameValuePair> pairs, String method)
throws ClientProtocolException, IOException,
AuthenticationException {
pairs.add(new BasicNameValuePair(KEY_SECURITY, KEY));
pairs.add(new BasicNameValuePair(LANG_KEY, lang));
pairs.add(new BasicNameValuePair(OS_KEY, OS));
pairs.add(new BasicNameValuePair(MODEL_KEY, model));
pairs.add(new BasicNameValuePair(CARRIER_KEY, carrier));
DefaultHttpClient client = getClient();
HttpPost post = new HttpPost();
try {
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
}
URI uri = URI.create(method);
post.setURI(uri);
client.addRequestInterceptor(preemptiveAuth, 0);
HttpHost target = new HttpHost(host, port, protocol);
HttpContext httpContext = new BasicHttpContext();
HttpResponse response = client.execute(target, post, httpContext);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 401) {
throw new AuthenticationException("Invalid username or password");
}
return response.getEntity();
}
我们终于发现了问题。这不是代码相关的。
这是反向DNS超时。因为我没有收到来自反向DNS的任何答案,所以我的apache/ssl会话被提前关闭。
通过使用谷歌的DNS在一个有根的设备上工作。
现在要做的唯一事情就是修复我们的反向DNS。
这里是一个解决办法:http://code.google.com/p/android/issues/detail?id=13117#c14
请拨打DefaultHttpClient或AndroidHttpClient实例此方法。它将阻止反向DNS查找。
private void workAroundReverseDnsBugInHoneycombAndEarlier(HttpClient client) {
// Android had a bug where HTTPS made reverse DNS lookups (fixed in Ice Cream Sandwich)
// http://code.google.com/p/android/issues/detail?id=13117
SocketFactory socketFactory = new LayeredSocketFactory() {
SSLSocketFactory delegate = SSLSocketFactory.getSocketFactory();
@Override public Socket createSocket() throws IOException {
return delegate.createSocket();
}
@Override public Socket connectSocket(Socket sock, String host, int port,
InetAddress localAddress, int localPort, HttpParams params) throws IOException {
return delegate.connectSocket(sock, host, port, localAddress, localPort, params);
}
@Override public boolean isSecure(Socket sock) throws IllegalArgumentException {
return delegate.isSecure(sock);
}
@Override public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException {
injectHostname(socket, host);
return delegate.createSocket(socket, host, port, autoClose);
}
private void injectHostname(Socket socket, String host) {
try {
Field field = InetAddress.class.getDeclaredField("hostName");
field.setAccessible(true);
field.set(socket.getInetAddress(), host);
} catch (Exception ignored) {
}
}
};
client.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", socketFactory, 443));
}
它影响nio连接? – Kai 2012-01-27 20:48:14
@凯,不知道,从来没有测试过。 – 2012-01-27 23:54:06
您是否尝试过在HttpClient on Android : NoHttpResponseException through UMTS/3G中提到的解决方案?
复制下面
我终于摆脱了这个问题:一个简单的HTTP标头竟被通过道路上的鱿鱼服务器处理:
期望:100继续
在android SDK上,它似乎默认使用DefaultHttpClient。为了解决这个问题,只需在您的代码:
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
我在我的问题中发布了3个答案,他们都不工作,其中一个就是这个。此解决方案不适合我。 – 2011-06-09 20:17:40
哦,我必须打开一些其他的链接,我的坏 – 2011-06-09 20:26:57
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
参考此代码为HTTP连接
这是相当无用的,因为代码崩溃httpclient.execute() – 2011-06-10 12:55:56
becoz在HttpPost设置它自己的网址比工作正常 – himanshu 2011-09-19 11:38:45
看看我的代码,我设置它无论如何,看看我的答案,解决了我的问题。 – 2011-09-19 16:36:47
您可以添加一些相关的代码,也许可以帮助。 – 2011-06-09 20:26:21
@Nicklas A,添加了一些代码。如果需要某些具体的东西,请问。 – 2011-06-09 20:43:40