java 呼叫http介面兩種方式
阿新 • • 發佈:2018-12-31
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
/**
*
* @author 金訊通12345介面呼叫
*
*/
public class HttpKxtUtils {
/** * HttpClient 處理方式 * @param content 傳送請求的內容 字元型別 TokenKey=cec084dfd89a0efe2bf742f7c98f7e8625547460&Name=anyd&Telephone=13594866323&Title=5554545&Content=ddd&Source=2&DataID=1111111111&TypeIndex=0&ImageList=2565 * @param url 請求地址 * @return * @author anyd */ public static String httpClientDoPost(String url,String content){ try { // 將引數轉為二進位制流 byte[] requestBytes = content.getBytes("utf-8"); HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); // 設定請求頭 Content-Type postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length); RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length, "application/json; charset=utf-8"); // 請求體 postMethod.setRequestEntity(requestEntity); httpClient.executeMethod(postMethod);// 執行請求 InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 獲取返回的流 byte[] datas = null; try { datas = readInputStream(soapResponseStream);// 從輸入流中讀取資料 } catch (Exception e) { e.printStackTrace(); } String result = new String(datas, "UTF-8");// 將二進位制流轉為String return result; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 從輸入流中讀取資料 * * @param inStream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } /** * HttpURLconnection 處理方式 * @param urlPath * @param content * @return */ public static String httpURLDoPost(String urlPath,String content){ try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");//設定本次請求的方式 conn.setReadTimeout(5000);//設定請求時間 conn.setDoInput(true);//是否開啟輸入流 預設為 true conn.setDoOutput(true);//是否開啟輸出流 預設為false //設定請求的內容型別 conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); conn.connect();//表示連線 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); writer.write(content); writer.close(); int code = conn.getResponseCode(); String result = ""; if(code ==HttpURLConnection.HTTP_OK ){ InputStream inputStream = conn.getInputStream(); byte[] datas = readInputStream(inputStream);// 從輸入流中讀取資料 result = new String(datas, "UTF-8");// 將二進位制流轉為String }else{ System.out.println(urlPath+"連線失敗"); result = urlPath+"連線失敗"; } return result; } catch (Exception e) { e.printStackTrace(); } return null; }
}