Java > HTTP Post

更新日 2014-02-16
広告
apache http client 4.xを使って、HTTP Postをする例です。
public HttpResponse post(String url, String data) throws Exception {

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost post = new HttpPost(url);
		
    StringEntity reqEntity = new StringEntity(data, "UTF-8");
    reqEntity.setContentType("application/xml");		
    post.setEntity(reqEntity);
		
    HttpResponse response = httpClient.execute(post);
		
    System.out.println(response.getStatusLine().getStatusCode()); // http status code
    System.out.println(EntityUtils.toString(httpResponse.getEntity())); // response body data

    return response;
}
広告
お問い合わせは sweng.tips@gmail.com まで。
inserted by FC2 system