Bre*_*art 12 ssl https android
我试图通过从Android应用程序向PHP服务器发布数据来将记录插入MySQL.我已经向AndroidManifest.xml添加了INTERNET权限
我明白了 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Android代码
private void senddata(ArrayList<NameValuePair> data)
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://10.0.2.2/insert222.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
}
catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error: "+e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
警告:不要在您不会完全信任的网络上使用的生产代码中实现此功能.特别是在公共互联网上的任何事情.此链接提供更正确的答案.这是使用SSL的实现.
您的问题是您正在使用DefaultHttpClient https(安全网址).
创建自定义DefaultHttpClient
public static HttpClient createHttpClient()
{
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
return new DefaultHttpClient(conMgr, params);
}
Run Code Online (Sandbox Code Playgroud)
比如下更改您的代码:
HttpClient httpclient = createHttpClient();
HttpPost httppost = new HttpPost("https://10.0.2.2/insert222.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)
如果你有问题
,看看这里
它应该工作.
| 归档时间: |
|
| 查看次数: |
33246 次 |
| 最近记录: |