Http發送post(x-www-form-urlencoded)請求


 

使用jar包:

<!--commons-httpclient-->
<dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
   </dependency>
</dependencies>

代碼:

try { String postURL = "http://*.*.*.*:8080/cjs/ad"; PostMethod postMethod = null; postMethod = new PostMethod(postURL) ;
       //添加請求頭數據 postMethod.setRequestHeader(
"Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ; //參數設置,需要注意的就是里邊不能傳NULL,要傳空字符串 NameValuePair[] data = { new NameValuePair("_ArgStr1","*"),
new NameValuePair("_ArgStr2","*") }; postMethod.setRequestBody(data); org.apache.commons.httpclient.HttpClient httpClient
= new org.apache.commons.httpclient.HttpClient(); int response = httpClient.executeMethod(postMethod); // 執行POST方法 String result = postMethod.getResponseBodyAsString() ; System.out.println(response); System.out.println(result); } catch (Exception e) { // logger.info("請求異常"+e.getMessage(),e); throw new RuntimeException(e.getMessage()); }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM