格式化

廣告

廣告2

2013年7月21日 星期日

[Android app開發基本概念] https post連線請求建立方法教學


Https與Http類似,只不過Https一般是通過post請求服務器,但是Https與http不同的是Https與服務器會話是處於連接狀態。http則發送請求後連接就會斷開。

另外使用HttpsURLConnection時需要實現HostnameVerifier 和 X509TrustManager,這兩個實現是必須的,要不會報安全驗證異常。然後初始化X509TrustManager中的SSLContext, 為javax.net.ssl.HttpsURLConnection設置默認的SocketFactory和HostnameVerifier。

之後最好要用UrlEncodedFormEntity()方法告訴伺服器 ,你傳進的那些參數值字串的編碼為何。預設的編碼是 ISO-8859-1,不傳的話會把你的傳入的UTF-8中文搞亂了。

 先自己重寫一個SSLSocketFactory類別:

package com.unood.eaccounts.base;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

public class SSL extends SSLSocketFactory
{

 SSLContext mSSLContext = SSLContext.getInstance("TLS");

 @Override
 public Socket createSocket() throws IOException
 {
  return mSSLContext.getSocketFactory().createSocket();
 }

 @Override
 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException
 {
  return mSSLContext.getSocketFactory().createSocket(socket, host, port, autoClose);
 }

 public SSL(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException
 {
  super(truststore);
  TrustManager mTrustManager = new X509TrustManager()
  {

   @Override
   public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
   {}

   @Override
   public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
   {}

   @Override
   public X509Certificate[] getAcceptedIssuers()
   {
    return null;
   }
  };
  mSSLContext.init(null, new TrustManager[]
  { mTrustManager }, null);
 }

 public static HttpClient createMyHttpClient()
 {
  try
  {
   KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
   trustStore.load(null, null);
   SSLSocketFactory mSSLSocketFactory = new SSL(trustStore);
   mSSLSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
   HttpParams params = new BasicHttpParams();
   HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
   SchemeRegistry registry = new SchemeRegistry();
   registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
   registry.register(new Scheme("https", mSSLSocketFactory, 443));
   ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
   return new DefaultHttpClient(ccm, params);
  }
  catch (KeyStoreException e)
  {
   e.printStackTrace();
  }
  catch (NoSuchAlgorithmException e)
  {
   e.printStackTrace();
  }
  catch (CertificateException e)
  {
   e.printStackTrace();
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  catch (KeyManagementException e)
  {
   e.printStackTrace();
  }
  catch (UnrecoverableKeyException e)
  {
   e.printStackTrace();
  }
  return new DefaultHttpClient();
 }
}

然後再你想要做https連線請求post的地方做以下操作;
   
HttpClient client = new DefaultHttpClient();
client = SSL.createMyHttpClient();
HttpPost post = new HttpPost("https://網址");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("參數名稱", "參數值"));
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = client.execute(post);

沒有留言 :