private static HashMap<String,String> CookieContiner=new HashMap<String,String>() ;
/**
* 保存Cookie
* @param resp
*/
public void SaveCookies(HttpResponse httpResponse)
{
Header[] headers = httpResponse.getHeaders("Set-Cookie");
String headerstr=headers.toString();
if (headers == null)
return;
for(int i=0;i<headers.length;i++)
{
String cookie=headers[i].getValue();
String[]cookievalues=cookie.split(";");
for(int j=0;j<cookievalues.length;j++)
{
String[] keyPair=cookievalues[j].split("=");
String key=keyPair[0].trim();
String value=keyPair.length>1?keyPair[1].trim():"";
CookieContiner.put(key, value);
}
}
}
/**
* 增加Cookie
* @param request
*/
public void AddCookies(HttpPost request)
{
StringBuilder sb = new StringBuilder();
Iterator iter = CookieContiner.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = entry.getKey().toString();
String val = entry.getValue().toString();
sb.append(key);
sb.append("=");
sb.append(val);
sb.append(";");
}
request.addHeader("cookie", sb.toString());
}
----------------------------------------
cookie rejected 解决方法
/**
* 获得httpclient实例
* @param hparams
* @return
*/
public HttpClient getHttpClient(){
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCookieSpecs().register("easy", csf);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy");
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 2000);
return httpClient;
}
// customer cookie policy, ignore cookie check
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
@Override
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
// Oh, I am easy
}
};
}
};
-------------------------------------------------
SSLException
遇到服务器SSL证书无效会出现哪些异常?有如何处理?
正常情况下,如果SSL证书无效,那么java会抛出SSLException。在Oauth授权的时候,如果碰到SSLException异常,我们就无法获取oauth_token和oauth_token_secret了。
解决方法很简单:那就是直接信任所有的证书,而不去检查证书的有效性。下面有个网上提供的类,使用的时候只要在联网前调用该类的allowAllSSL()函数即可。
- public class _FakeX509TrustManager implements X509TrustManager {
-
- private static TrustManager[] trustManagers;
- private static final X509Certificate[] _AcceptedIssuers = new
- X509Certificate[] {};
-
- @Override
- public void checkClientTrusted(X509Certificate[] chain, String
- authType) throws CertificateException {
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] chain, String
- authType) throws CertificateException {
- }
-
- public boolean isClientTrusted(X509Certificate[] chain) {
- return true;
- }
-
- public boolean isServerTrusted(X509Certificate[] chain) {
- return true;
- }
-
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- return _AcceptedIssuers;
- }
-
- public static void allowAllSSL() {
- HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
-
- @Override
- public boolean verify(String arg0, SSLSession arg1) {
-
- return true;
- }
-
- });
-
- SSLContext context = null;
- if (trustManagers == null) {
- trustManagers = new TrustManager[] { new _FakeX509TrustManager() };
- }
-
- try {
- context = SSLContext.getInstance("TLS");
- context.init(null, trustManagers, new SecureRandom());
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
-
- HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
- }
-
- }
使用实例:
_FakeX509TrustManager.allowAllSSL();//调用
URL postUrl = new URL("https://open.t.qq.com/cgi-bin/request_token?"+params);
URL postUrl = new URL("http://www.linuxidc.com");
HttpURLConnection con = (HttpURLConnection) postUrl.openConnection();