Android中的HttpsURLConnection连接
[java]
package com.LeadingGH.Core;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.*;
import android.preference.PreferenceManager;
import com.LeadingGH.Util.Base64;
public class VPNAuth
{
public String url = "";
public String cookie = "";
private myX509TrustManager xtm = new myX509TrustManager();
private myHostnameVerifier hnv = new myHostnameVerifier();
public VPNAuth(String loginName,String loginPwd,String urlAddress)
{
SSLContext sslContext = null;
if(loginName.equals("") || urlAddress.equals(""))
return;
try
{
url = urlAddress+"/SSL/user/PasswordUserLogin?Integrate=true&LoginName="
+ Base64.encode(loginName.getBytes())
+ "&SMS=false&AutoLogin=false&IsTwoAuth=0&LoginPassword="
+ Base64.encode(loginPwd.getBytes())
+ "&APCCode=4567&APCPassword=4567";
sslContext = SSLContext.getInstance("SSL"); // 或SSL
X509TrustManager[] xtmArray = new X509TrustManager[]{ xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
}
catch (GeneralSecurityException e)
{
e.printStackTrace();
}
if (sslContext != null)
{
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
run(url,"");
}
public void VPNLogOut()
{
String strUrl = "https://58.16.4.27/SSL/user/UserLogout?SMS=false&CS=false&Close=false&Name="+Base64.encode("test".getBytes());
run(strUrl,"");
cookie= "";
}
public String run(String strUrl,String postXML)
{
HttpsURLConnection urlCon = null;
URL urlInstance = null;
String firstCookie = "";
String returnVal = "";
StringBuffer sb = new StringBuffer();
try
{
String params = URLEncoder.encode(postXML);
params = "AcceptXML=" + params;
urlInstance = new URL(strUrl);
urlCon = (HttpsURLConnection) (urlInstance).openConnection();
urlCon.setInstanceFollowRedirects(false);
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setRequestMethod("POST");
urlCon.setRequestProperty("Cookie", cookie);
urlCon.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
urlCon.setRequestProperty("Connection", "Keep-Alive");
urlCon.connect();
byte[] bpara = params.toString().getBytes();
urlCon.getOutputStream().write(bpara, 0, bpara.length);
urlCon.getOutputStream().flush();
urlCon.getOutputStream().close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlCon.getInputStream()));
String redictURL = urlCon.getHeaderField(
补充:移动开发 , Android ,