public class HttpTest(){
public void testHttpPost() throws Exception{
//1. 創建一個客戶端(瀏覽器)
HttpClient httpClient = HttpClientBuilder.create().build();
//2. 初始化post請求的過程
String url = "www.baidu.com"
HttpPost post = new HttpPost("url");
//3.傳遞參數
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("arg0", "1"));
nvps.add(new BasicNameValuePair("arg1", "2"));
post.setEntity(new UrlEncodedFormEntity(nvps));
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//4. 用剛纔創建的客戶端去執行post請求
//webservice SOAP
HttpResponse response = httpClient.execute(post);
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
//JSONObject jsonObject = JSONObject.fromObject(result);
System.out.println(result);
}
}
Java中HttpPost請求流程--4.5.2版本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.