Je me suis référé à la page suivante. Exemple de communication HTTP Java (HttpClient)
Téléchargez et décompressez le fichier suivant. httpcomponents-client-4.5.6-bin.tar.gz
Http_get.java
// -----------------------------------------------------------------------
/*
Http_get.java
Oct/12/2018
*/
// -----------------------------------------------------------------------
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
*Exemple HttpClients
*/
class Uri_get
{
static String uri_get_proc(String uri)
{
String res = "";
Charset charset = StandardCharsets.UTF_8;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet request = new HttpGet(uri);
System.out.println
("Exécution de la demande "" + request.getRequestLine() + "」");
CloseableHttpResponse response = null;
try {
response = httpclient.execute(request);
int status = response.getStatusLine().getStatusCode();
System.out.println("État HTTP:" + status);
//État HTTP: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_get
{
public static void main(String[] args)
{
String uri = "http://httpbin.org/get";
String res = Uri_get.uri_get_proc(uri);
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_get.class: Http_get.java
javac -cp $(HTTPCLIENT_JAR) Http_get.java
clean:
rm -f *.class
Commande d'exécution
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_get
Résultat d'exécution
Exécution de la demande "GET http://httpbin.org/get HTTP/1.1」
État HTTP:200
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.6 (Java/10.0.2)"
},
"origin": "180.3.100.78",
"url": "http://httpbin.org/get"
}
Faire la même chose avec curl
$ curl http://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1"
},
"origin": "180.3.100.78",
"url": "http://httpbin.org/get"
}
Recommended Posts