首先
用HttpClient去模擬發送請求時
a
在查找相關資料時
b
擴展HttpClient 類實現自動接受證書
具體的步驟如下
?提供一個自定義的socket factory (test
mons
X
?創建一個mons
Protocol myhttps = new Protocol(
?注冊剛才創建的https 協議對象
Protocol
具體代碼如下
sslcontext
{ new TrustAnyTrustManager() }
}
catch (NoSuchAlgorithmException e)
{
e
}
catch (KeyManagementException e)
{
e
}
return sslcontext;
}
private SSLContext getSSLContext()
{
if (this
{
this
}
return this
}
public Socket createSocket(Socket socket
boolean autoClose) throws IOException
{
return getSSLContext()
port
}
public Socket createSocket(String host
UnknownHostException
{
return getSSLContext()
}
public Socket createSocket(String host
int clientPort) throws IOException
{
return getSSLContext()
clientHost
}
public Socket createSocket(String host
int localPort
UnknownHostException
{
if (params == null)
{
throw new IllegalArgumentException(
}
int timeout = params
SocketFactory socketfactory = getSSLContext()
if (timeout ==
{
return socketfactory
localPort)
}
else
{
Socket socket = socketfactory
SocketAddress localaddr = new InetSocketAddress(localAddress
localPort)
SocketAddress remoteaddr = new InetSocketAddress(host
socket
nnect(remoteaddr
return socket;
}
}
// 自定義私有類
private static class TrustAnyTrustManager implements X
{
public void checkClientTrusted(X
throws CertificateException
{
}
public void checkServerTrusted(X
throws CertificateException
{
}
public X
{
return new X
{};
}
}
下面的是httpClient的具體實現類
private static HttpClient httpClient = null;
static
{
//指定協議名稱和默認的端口號
Protocol myhttps = new Protocol(
//注冊剛才創建的https 協議對象
Protocol
httpClient = new HttpClient()
}
/**
* 發送請求報文
* @param url
*
登錄請求URL
* @param pList
*
是否包含請求參數
* @return
* @throws UnsupportedEncodingException
*/
public static String doRequestToString(String url
{
//獲得postMethod對象
PostMethod pmethod = getPostMethod(url)
pmethod
//判斷是否包含參數
if(null != pList && pList
{
pmethod
}
String value
try
{
(pmethod)
value = pmethod
}
catch ( HttpException e )
{
e
}
catch ( IOException e )
{
e
}
return value;
}
/**
* 獲得
* @param url
*
請求URL
* @param filePath
*
驗證碼保存路徑 如
* @return
*/
public static File doGetFile(String url
{
PostMethod pmethod = getPostMethod(url)
pmethod
try
{
(pmethod)
//得到響應中的流對象
InputStream in = pmethod
//包裝 並讀出流信息
BufferedInputStream bis = new BufferedInputStream(in)
File file = new File(filePath)
FileOutputStream fs = new FileOutputStream(file)
byte[] buf = new byte[
int len = bis
if(len ==
file
file = null;
}
while (len !=
fs
len = bis
}
fs
fs
return file;
}
catch (HttpException e)
{
e
}
catch (IOException e)
{
e
}
return null;
}
public static List<NameValuePair> createNameValuePair(String params) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>()
if (null != params && !params
String[] _params = params
// userCookieList = new AttributeList()
for (int i =
int _i = _params[i]
if (_i !=
String name = _params[i]
String value = _params[i]
nvps
}
}
}
return nvps;
}
public static PostMethod getPostMethod(String url)
{
PostMethod pmethod = new PostMethod(url)
//設置響應頭信息
pmethod
pmethod
pmethod
pmethod
return pmethod;
}
模擬請求的類已經出來
當randError為
現在最後一步就是拼接參數了
/**
* 獲取驗證碼
* @param filePath
* @return
*/
public static String getRandCode(String filePath)
{
String randCode =
/** 獲取驗證碼 */
HttpDoPostUtils
randCode = readString(
return randCode;
}
/**
* 實現登錄操作
* @throws UnsupportedEncodingException
*/
public static void doLogin() throws UnsupportedEncodingException
{
String randCode = getRandCode(
/** 登錄前 提交得到報文 */
String loginBeforeVal = HttpDoPostUtils
//將返回的JSON報文轉換成指定的對象
JSONObject jsonObj = JSONObject
LoginBeforeValidatior loginBefore = new LoginBeforeValidatior()
loginBefore = (LoginBeforeValidatior) JSONObject
//拼接參數
StringBuffer params = new StringBuffer()
params
//像服務器發送登錄請求 並返回對應的報文
String loginResponseText = HttpDoPostUtils
System
}
/**
* 多控制台讀取驗證碼
* @param msg
* @return
* @throws Exception
*/
private static String readString(String msg)
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System
try{
System
return bufferedReader
}catch(Exception e){
}
return
}
public static void main(String[] args) throws UnsupportedEncodingException
{
//Login login = new Login()
//login
Login
}
URL都是在配置文件中的
通過返回的HTML
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25921.html