您的位置:首页 > 理论基础 > 计算机网络

android 2.3系统上HttpClient SSL请求 Error

2013-08-26 17:27 134 查看
先做搬运工:

解决办法:
http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https

有人改使用HttpUrlConnection:
http://stackoverflow.com/questions/9574870/no-peer-certificate-error-in-android-2-3-but-not-in-4

但是你可以看到这会在2.2系统上导致另外一个问题:
http://stackoverflow.com/a/5895320/912936

制作SSL签名证书:
http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

在做新浪微博认证时候遇到的问题,在这里吐槽下新浪微博。上个周五测试,测试人员发现原来的可以用代码崩了,debug发现原来用户授权后直接返回token ,uid...的Bundle只返回code一个键值对了。但是在下午解决该bug的时候,他又返回token,uid等等四个键值对。今天早上来发现,又只返回一个code了!!!不知道过一会还会不会改,最重要的是:竟然没有给开发人员任何通知!!!!!!

新浪微博通过code获取token部分代码:

String url = "https://api.weibo.com/oauth2/access_token?";
//获取了code,但是访问https://api.weibo.com/oauth2/access_token?
// client_id=id
// &client_secret=secret
// &grant_type=authorization_code
// &redirect_uri=http://www.xx.xx
// &code=7efexxxxxxxxxxx(code是WeiboAuthListener   中的omComplete中获取到code,如下面:)
/*****
WeiboAuthListener   中的omComplete中获取到code
@Override
public void onComplete(Bundle values) {
String code = values.getString("code");
*****//

HttpClient httpClient = new DefaultHttpClient(clientConnManager, params);//两个参数上面的链接中已经有明确定义,可以视自己需要而定clientConnManager。

//通过code获取token
List<NameValuePair> valuePairList = AppUtil.httpVerifyParams();
valuePairList.add(new BasicNameValuePair("client_id", Config.SINA_APP_ID));
valuePairList.add(new BasicNameValuePair("client_secret", Config.SINA_APP_SECRET));
valuePairList.add(new BasicNameValuePair("grant_type", "authorization_code"));
valuePairList.add(new BasicNameValuePair("redirect_uri", Config.SINA_REDIRECT_URL));
valuePairList.add(new BasicNameValuePair("code", code));
//听说必须用post请求,具体我也没找到官方的文档。
HttpPost httpUriRequest = new HttpPost(url);
HttpPost httpPost = (HttpPost) httpUriRequest;
httpPost.setEntity(new UrlEncodedFormEntity(valuePairList, HTTP.UTF_8));

//获取返回结果
private String connectionNet(HttpUriRequest httpUriRequest) throws IOException {

try {
HttpResponse response = httpClient.execute(httpUriRequest);
if (response.getStatusLine().getStatusCode() == 200) {
Logger.i("request service success,process the result to string ...");
//网络请求成功:
String str = EntityUtils.toString(response.getEntity());
return str;
}
return null;
} catch (IOException e) {
e.printStackTrace();
Logger.e("#ConnectionNetWorkAsyncTask# is error when connection the network");
} finally {
httpUriRequest.abort();
httpClient.getConnectionManager().shutdown(); //如果是多次调用该方法从网络获取数据,请不要在这里调用shutdown方法。
}
return null;
}
/*******************华丽丽的灰阁线*****************************/
3.0以上系统可以直接下面这样:
SchemeRegistry supportedSchemes = new SchemeRegistry();
supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
/************************************************************/

//Android新人们,一定不要在主线程中访问网络,切记切记


希望有用于正遇到这个问题的人。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: