I referred to the next page. Java HTTP Communication Sample (HttpClient)
Download and unzip the following files. httpcomponents-client-4.5.6-bin.tar.gz
Http_post.java
// -----------------------------------------------------------------------
/*
Http_post.java
Oct/09/2018
*/
// -----------------------------------------------------------------------
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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;
// -----------------------------------------------------------------------
class Uri_get {
static String uri_post_proc(String uri,List<NameValuePair> params)
{
String res = "";
Charset charset = StandardCharsets.UTF_8;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost request = new HttpPost(uri);
System.out.println
("Execution of request "" + request.getRequestLine() + "」");
CloseableHttpResponse response = null;
try {
request.setEntity(new UrlEncodedFormEntity(params));
response = httpclient.execute(request);
int status = response.getStatusLine().getStatusCode();
System.out.println("HTTP status:" + status);
//HTTP status:200
if (status == HttpStatus.SC_OK){
res = EntityUtils.toString(response.getEntity(),charset);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
}
// -----------------------------------------------------------------------
public class Http_post
{
public static void main(String[] args)
{
String uri = "http://httpbin.org/post";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("user","jiro"));
params.add(new BasicNameValuePair("password","123456"));
String res = Uri_get.uri_post_proc(uri,params);
System.out.println(res);
}
}
// -----------------------------------------------------------------------
Makefile
LIB=../httpcomponents-client-4.5.6/lib
HTTPCLIENT_JAR=.:$(LIB)/httpclient-4.5.6.jar:$(LIB)/httpcore-4.4.10.jar
Http_post.class: Http_post.java
javac -cp $(HTTPCLIENT_JAR) Http_post.java
clean:
rm -f *.class
Execution command
LIB=../httpcomponents-client-4.5.6/lib
HTTPCLIENT_JAR=.:$LIB/httpclient-4.5.6.jar:$LIB/httpcore-4.4.10.jar:$LIB/commons-logging-1.2.jar
#
java -cp $HTTPCLIENT_JAR Http_post
Execution result
Execution of request "POST http://httpbin.org/post HTTP/1.1」
HTTP status:200
{
"args": {},
"data": "",
"files": {},
"form": {
"password": "123456",
"user": "jiro"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "25",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.6 (Java/10.0.2)"
},
"json": null,
"origin": "180.3.100.78",
"url": "http://httpbin.org/post"
}
If you do the same with curl
$ curl -X POST http://httpbin.org/post -d 'user=jiro&password=123456'
{
"args": {},
"data": "",
"files": {},
"form": {
"password": "123456",
"user": "jiro"
},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Content-Length": "25",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1"
},
"json": null,
"origin": "180.3.100.78",
"url": "http://httpbin.org/post"
}
Here is a sample of Get. How to use Java HttpClient (Get)
Recommended Posts