使用java模拟HTTP请求
package hk.sohan.web.controller;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import hk.sohan.core.util.RSAUtils;
import hk.sohan.web.constant.AppConstants;
public class TestRequest {
/**
* 接口调用 GET
*/
public static void httpURLConectionGET(String urls) {
try {
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
connection.disconnect();
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
}
/**
* 接口调用 POST
*/
public static void httpURLConnectionPOST (String urls) {
try {
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
String parm = "storeId=" + URLEncoder.encode("32", "utf-8");
dataout.writeBytes(parm);
dataout.flush();
dataout.close();
System.out.println(connection.getResponseCode());
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = bf.readLine()) != null) {
sb.append(bf.readLine());
}
bf.close();
connection.disconnect();
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String param = "biz_paras={\"tmallOrderId\":\"HJKL\",\"iccid\":\"78NM\"}&format=json&method=method_name&seller_id=12345678&sign_method=RSA×tamp=20150309135203528&v=1.0&app_key=tmall&operator_id=alicom&access_token=999888";
String sign = null;
try {
sign = RSAUtils.signByPrivateKey(AppConstants.RSAPRIVATE_KEY.getBytes("utf-8"), param.getBytes("utf-8"));
System.out.println("第一次使用私钥加签=="+sign);
System.out.println(RSAUtils.verifyByPublicKey(AppConstants.RSAPublicKey.getBytes(), param.getBytes(), sign.getBytes()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpURLConectionGET("http://192.168.0.84:8080/tmallwt/tmall/checkOrderSim?sign=bI6ypR13qSwhrDvIPIPFxZScrej6xT48fm5hCyD9yf29iMSoup7VBgN2BSaaVmWRv3uz7FNdZyM3qLCkMuN9yby4RI4BlWMKK2g2fBZ9TlvySS+2KV+At9S3BKVGMTTiRlusEMS5GmaL06ckJDwsUU5x2ZGGqblMOgykwpr251U=&biz_paras={\"tmallOrderId\":\"HJKL\",\"iccid\":\"78NM\"}&format=json&method=method_name&seller_id=12345678&sign_method=RSA×tamp=20150309135203528&v=1.0&app_key=tmall&operator_id=alicom&access_token=999888");
}
}
/**
* http://192.168.0.84:8080/tmallwt/tmall/checkOrderSim?biz_paras={'tmallOrderId':HJKL,'iccid':78NM
*
* http://192.168.0.84:8080/tmallwt/tmall/checkOrderSim?sign=5364C12B837RE89D256DB2A301A3E×tamp=20150309135203528&v=1.0&sell_id=12345678&method=method_name&format=json&biz_paras={"billId":"13800000000","queryType":"1"}&sign_method= RSA&app_key=tmall&operator_id=alicom&access_token=999888
*
*
*/