Javaのhttpclientでリクエストをpost、get使い方

package com.spring.httpclient;

import java.io.IOException;

public abstract class BaseRequestTest {

private final static RequestConfig config;
private final static HttpClient client;
static {
config = RequestConfig.custom().setConnectionRequestTimeout(5000)
.setConnectTimeout(5000).setSocketTimeout(5000).build();
client = HttpClientBuilder.create().setDefaultRequestConfig(config)
.build();
}
protected String uri;
private HttpRequestBase hrb;
protected Header[] headers;

protected List<NameValuePair> parameters;

public abstract void setUri();

public abstract void setParameters();

public void setHrb() {

}

public void setHeaders() {
}

public void getHrb() {
if (this.hrb == null)
this.hrb = new HttpPost(uri);
this.hrb.setConfig(config);
}

public void getHeaders() {
if (ArrayUtils.isNotEmpty(headers)) {
((HttpPost) this.hrb).setHeaders(headers);
}
}

public void getParameters() throws ParseException, IOException {
if (this.hrb instanceof HttpPost)
((HttpPost) this.hrb).setEntity(new UrlEncodedFormEntity(
parameters, Consts.UTF_8));
else
uri += EntityUtils.toString(new UrlEncodedFormEntity(parameters,
Consts.UTF_8));
}

public void test1() throws ParseException, IOException {
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
NameValuePair nvp1 = new BasicNameValuePair(“username", “li");
NameValuePair nvp2 = new BasicNameValuePair(“password", “123456");
NameValuePair nvp3 = new BasicNameValuePair(“age", “23");
parameters.add(nvp1);
parameters.add(nvp2);
parameters.add(nvp3);
UrlEncodedFormEntity efe = new UrlEncodedFormEntity(parameters,
Consts.UTF_8);
System.out.println(EntityUtils.toString(efe));
}

public void get() {

CloseableHttpClient client = HttpClientBuilder.create().build();
String uri = “http://www.arkgame.com";

HttpGet request = new HttpGet(uri);
request.setConfig(config);

try {
CloseableHttpResponse response = client.execute(request);
// ステータス
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);

// タイプ
HttpEntity entity = response.getEntity();
String mimeType = ContentType.getOrDefault(entity).getMimeType();
System.out.println(mimeType);

// コンテンツ
String string = EntityUtils.toString(entity);
System.out.println(string);

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void post() {

CloseableHttpClient client = HttpClientBuilder.create().build();
String uri = “http://www.arkgame.com";

HttpPost request = new HttpPost(uri);
request.addHeader(HttpHeaders.ACCEPT, “application/xml");
request.setConfig(config);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
try {
request.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
CloseableHttpResponse response = client.execute(request);
// ステータス
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);

// タイプ
HttpEntity entity = response.getEntity();
String mimeType = ContentType.getOrDefault(entity).getMimeType();
System.out.println(mimeType);

// コンテンツ
String string = EntityUtils.toString(entity);
System.out.println(string);

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void execute() throws ClientProtocolException, IOException {
setUri();
setParameters();

getHrb();
getHeaders();
getParameters();
HttpResponse response = client.execute(hrb);
this.statusCode = response.getStatusLine().getStatusCode();
this.mimeType = ContentType.getOrDefault(response.getEntity())
.getMimeType();
this.responseStr = IOUtils.toString(response.getEntity().getContent());
}

/**
* http://localhost:8080/springmvc-api/hellohttp://localhost:8080/springmvc-
* api/hellohttp://localhost:8080/springmvc-api/hello 戻りスタータス
*/
private int statusCode;
/**
* 戻りタイプ
*/
private String mimeType;

private String responseStr;

public int getStatusCode() {
return statusCode;
}

public String getMimeType() {
return mimeType;
}

public String getResponseStr() {
return responseStr;
}

}

Development

Posted by arkgame