https请求样例

来源:互联网 发布:linux抓包工具 编辑:程序博客网 时间:2024/06/06 01:41
public static String sendPost(String str)  throws Exception{
StringBuffer response = new StringBuffer();
int responseCode = -1; 
//String postUrl = "http://111.13.56.193:9003/axj_http_sms/sms";
String postUrl = ConfigContainer.getSendurl(); 
try {
       SSLContext sc;
  sc = SSLContext.getInstance("SSL");
  sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
        new java.security.SecureRandom());
URL obj = new URL(postUrl);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
// String urlParameters = mt.toString();
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
con.setSSLSocketFactory(sc.getSocketFactory());
con.setHostnameVerifier(new TrustAnyHostnameVerifier());
con.setUseCaches(false);
con.setDoOutput(true);
// DataOutputStream wr = new
// DataOutputStream(con.getOutputStream());
OutputStream out = con.getOutputStream();
out.write(str.getBytes("UTF-8"));
out.flush();
out.close();
responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("\t" + responseCode + "\t" + "POST" + "\t" + postUrl
+ "\t" + response.toString());
} catch (IOException e) {
// AppLogger.appDebug("###get发�?异常:"+responseCode+"\t"+"POST"+"\t"+mo.createUrlInfo()+"\t"+response.toString());
e.printStackTrace();

return response.toString();
}