httpclient post 傳參數

hollistertop 8年前發布 | 1K 次閱讀 Java HttpComponents

httpclient post 參數    

public static void main(String args[]) throws IOException {

    CloseableHttpClient client = HttpClients.createDefault();
    String url = "http://localhost:8888/QueryUser";
    HttpPost post = new HttpPost(url);
    String name= "mike";
    JSONObject arg = new JSONObject().element("age", 44).element("job", "cooker"));
    String queryCase = "name=" + name+ "&&arg=" + arg;
    StringEntity reqEntity = new StringEntity(queryCase);
    System.out.println(reqEntity);
    reqEntity.setContentType("application/x-www-form-urlencoded");
    post.setEntity(reqEntity);
    HttpResponse response = client.execute(post);

    if (response.getStatusLine().getStatusCode() == 200) {
        HttpEntity entity = response.getEntity();
        String message = EntityUtils.toString(entity, "utf-8");
        System.out.println(message);
    } else {
        System.out.println("請求失敗");
    }
}
 本文由用戶 hollistertop 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!