1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HttpClient 4.x : GET

Posted at
package hello.httpclient4x;

import java.net.URI;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HelloGetMain {

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

		CloseableHttpClient httpclient = HttpClients.createDefault();

		try {

			String url = "http://localhost:8080/HelloWeb/index.jsp";
			String method = "";

			ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("msg", "ハロー"));

			URI serviceUri = new URIBuilder(url + method) //
					.setParameters(nvps)//
					.build();

			System.err.println(serviceUri.toString());

			HttpGet httpget = new HttpGet(serviceUri);

			System.out.println("Executing request " + httpget.getRequestLine());

			CloseableHttpResponse response = httpclient.execute(httpget);

			// response.getEntity().getContent() -> InputStream

			try {
				System.err.println(response.getStatusLine());
				// EntityUtils.consume(response.getEntity());

				System.err.println(response.getStatusLine().getStatusCode());

				Header[] headers = response.getAllHeaders();
				for (Header header : headers) {
					System.err.println(header.getName() + ": "
							+ header.getValue());
				}

				HttpEntity httpEntity = response.getEntity();
				System.err.println(EntityUtils.toString(httpEntity));

			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
	}
}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?