https请求测试 姊妹篇HttpUrlConnection
package com.abin.lee.httpurl;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLDecoder;
import javax.net.ssl.HttpsURLConnection;
import junit.framework.TestCase;
public class GetPacketsTest extends TestCase{
private static final String HTTPSURL = "https://localhost:8444/stsf/mvc/mobile/ssl/getMessageState";
public void testGetPackets() throws Exception{
System.setProperty("javax.net.ssl.keyStore","D:\\home\\jiekou.keystore");
System.setProperty("javax.net.ssl.keyStorePassword","lcd_8888");
System.setProperty("javax.net.ssl.trustStore","D:\\home\\jiekou.truststore");
System.setProperty("javax.net.ssl.trustStorePassword","lcd_8888");
StringreqquestStr="terminalId="+java.net.URLEncoder.encode("sdf","utf-8")+"&userId=" +
java.net.URLEncoder.encode("1034","utf-8")+"&orderId="+
java.net.URLEncoder.encode("10344","utf-8")+"&payMoney="+
java.net.URLEncoder.encode("500","utf-8")+"&payCoin="+
java.net.URLEncoder.encode("40","utf-8")+"&payType="+
java.net.URLEncoder.encode("0","utf-8");
System.out.println(reqquestStr);
String result="";
URL target = new URL(HTTPSURL);
HttpsURLConnection httpConn =
(HttpsURLConnection)target.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false); //Post 请求不能使用缓存
httpConn.setRequestMethod("POST");//POST
httpConn.connect();
PrintWriter out = new PrintWriter(httpConn.getOutputStream());//写入请求参数
out.write(reqquestStr);
out.flush();
out.close();
System.out.println(httpConn.getResponseCode());
InputStream inputStream=httpConn.getInputStream();
InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
BufferedReader in = new BufferedReader(inputStreamReader);
String lineStr;
while ((lineStr = in.readLine())!= null)
{
result += lineStr;
result+="\r\n";
}
result=URLDecoder.decode(result, "UTF-8");
in.close();
System.out.println(result);
}
}