Reputation: 34725
In Java, How to compose an HTTP request message and send it to an HTTP web server?
Upvotes: 454
Views: 1181981
Reputation: 338171
Java 11 brought a new HTTP client. Contained in the new module java.net.http
. For more info, see:
Using the new HTTP Client is easy. The framework provides convenient fluent syntax.
First create the client object.
HttpClient client = HttpClient.newBuilder( )
.version( HttpClient.Version.HTTP_1_1 )
.build( ) ;
Set up the request.
HttpRequest request = HttpRequest.newBuilder( )
.uri( URI.create( "http://worldtimeapi.org/api/timezone/UTC" ) )
.timeout( Duration.ofMinutes( 1 ) )
.GET( )
.build( );
Hit the server.
HttpResponse < String > response = client.send( request , HttpResponse.BodyHandlers.ofString( ) );
Extract the response parts.
response.statusCode( )
response.body( )
Full example code:
package work.basil.example.web;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ExHttpClient
{
public static void main ( String[] args )
{
// JEP 321: HTTP Client API
// https://openjdk.org/jeps/321
try (
HttpClient client = HttpClient.newBuilder( )
.version( HttpClient.Version.HTTP_1_1 )
.build( ) ;
)
{
// Example HTTP server: http://worldtimeapi.org/
HttpRequest request = HttpRequest.newBuilder( )
.uri( URI.create( "http://worldtimeapi.org/api/timezone/UTC" ) )
.timeout( Duration.ofMinutes( 1 ) )
.GET( )
.build( );
HttpResponse < String > response = client.send( request , HttpResponse.BodyHandlers.ofString( ) );
System.out.println( response.statusCode( ) );
System.out.println( response.body( ) );
} catch ( IOException | InterruptedException e )
{
throw new RuntimeException( e );
}
}
}
When run:
200
{"utc_offset":"+00:00","timezone":"UTC","day_of_week":3,"day_of_year":248,"datetime":"2024-09-04T18:51:19.336832+00:00","utc_datetime":"2024-09-04T18:51:19.336832+00:00","unixtime":1725475879,"raw_offset":0,"week_number":36,"dst":false,"abbreviation":"UTC","dst_offset":0,"dst_from":null,"dst_until":null,"client_ip":"71.212.7.38"}
By the way, Java 18+ comes with a basic implementation of a HTTP Web server. See JEP 408: Simple Web Server. Caveat: This implementation is not meant for use in production; its purpose is learning and experimenting.
This simple HTTP server can be run from command-line in a console using a tool named jwebserver
. Or you can run the server from Java code.
So you could create your own little server to test your client code.
Upvotes: 0
Reputation: 24472
If you are using Java 11 or newer (except on Android), instead of the legacy HttpUrlConnection class, you can use Java 11 new HTTP Client API.
var uri = URI.create("https://httpbin.org/get?age=26&isHappy=true");
var client = HttpClient.newHttpClient();
var request = HttpRequest
.newBuilder()
.uri(uri)
.header("accept", "application/json")
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
The same request executed asynchronously:
var responseAsync = client
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println);
// responseAsync.join(); // Wait for completion
var request = HttpRequest
.newBuilder()
.uri(uri)
.version(HttpClient.Version.HTTP_2)
.timeout(Duration.ofMinutes(1))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer fake")
.POST(BodyPublishers.ofString("{ title: 'This is cool' }"))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
For sending form data as multipart (multipart/form-data
) or url-encoded (application/x-www-form-urlencoded
) format, see this solution.
See this article for examples and more information about HTTP Client API.
Upvotes: 15
Reputation: 21270
Here's a complete Java 7 program:
class GETHTTPResource {
public static void main(String[] args) throws Exception {
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("http://example.com/").openStream())) {
System.out.println(s.useDelimiter("\\A").next());
}
}
}
The new try-with-resources will auto-close the Scanner, which will auto-close the InputStream.
Upvotes: 30
Reputation: 308733
You can use java.net.HttpUrlConnection.
Example (from here), with improvements. Included in case of link rot:
public static String executePost(String targetURL, String urlParameters) {
HttpURLConnection connection = null;
try {
//Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
Upvotes: 340
Reputation: 3237
You may use Socket for this like
String host = "www.yourhost.com";
Socket socket = new Socket(host, 80);
String request = "GET / HTTP/1.0\r\n\r\n";
OutputStream os = socket.getOutputStream();
os.write(request.getBytes());
os.flush();
InputStream is = socket.getInputStream();
int ch;
while( (ch=is.read())!= -1)
System.out.print((char)ch);
socket.close();
Upvotes: 14
Reputation: 23164
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Upvotes: 249
Reputation: 32378
Google java http client has nice API for http requests. You can easily add JSON support etc. Although for simple request it might be overkill.
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import java.io.IOException;
import java.io.InputStream;
public class Network {
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public void getRequest(String reqUrl) throws IOException {
GenericUrl url = new GenericUrl(reqUrl);
HttpRequest request = HTTP_TRANSPORT.createRequestFactory().buildGetRequest(url);
HttpResponse response = request.execute();
System.out.println(response.getStatusCode());
InputStream is = response.getContent();
int ch;
while ((ch = is.read()) != -1) {
System.out.print((char) ch);
}
response.disconnect();
}
}
Upvotes: 16
Reputation: 971
There's a great link about sending a POST request here by Example Depot::
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}
If you want to send a GET request you can modify the code slightly to suit your needs. Specifically you have to add the parameters inside the constructor of the URL. Then, also comment out this wr.write(data);
One thing that's not written and you should beware of, is the timeouts. Especially if you want to use it in WebServices you have to set timeouts, otherwise the above code will wait indefinitely or for a very long time at least and it's something presumably you don't want.
Timeouts are set like this conn.setReadTimeout(2000);
the input parameter is in milliseconds
Upvotes: 7
Reputation: 76709
Apache HttpComponents. The examples for the two modules - HttpCore and HttpClient will get you started right away.
Not that HttpUrlConnection is a bad choice, HttpComponents will abstract a lot of the tedious coding away. I would recommend this, if you really want to support a lot of HTTP servers/clients with minimum code. By the way, HttpCore could be used for applications (clients or servers) with minimum functionality, whereas HttpClient is to be used for clients that require support for multiple authentication schemes, cookie support etc.
Upvotes: 57
Reputation: 3294
This will help you. Don't forget to add the JAR HttpClient.jar
to the classpath.
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class MainSendRequest {
static String url =
"http://localhost:8080/HttpRequestSample/RequestSend.jsp";
public static void main(String[] args) {
//Instantiate an HttpClient
HttpClient client = new HttpClient();
//Instantiate a GET HTTP method
PostMethod method = new PostMethod(url);
method.setRequestHeader("Content-type",
"text/xml; charset=ISO-8859-1");
//Define name-value pairs to set into the QueryString
NameValuePair nvp1= new NameValuePair("firstName","fname");
NameValuePair nvp2= new NameValuePair("lastName","lname");
NameValuePair nvp3= new NameValuePair("email","[email protected]");
method.setQueryString(new NameValuePair[]{nvp1,nvp2,nvp3});
try{
int statusCode = client.executeMethod(method);
System.out.println("Status Code = "+statusCode);
System.out.println("QueryString>>> "+method.getQueryString());
System.out.println("Status Text>>>"
+HttpStatus.getStatusText(statusCode));
//Get data as a String
System.out.println(method.getResponseBodyAsString());
//OR as a byte array
byte [] res = method.getResponseBody();
//write to file
FileOutputStream fos= new FileOutputStream("donepage.html");
fos.write(res);
//release connection
method.releaseConnection();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 15
Reputation: 269627
I know others will recommend Apache's http-client, but it adds complexity (i.e., more things that can go wrong) that is rarely warranted. For a simple task, java.net.URL
will do.
URL url = new URL("http://www.y.com/url");
InputStream is = url.openStream();
try {
/* Now read the retrieved document from the stream. */
...
} finally {
is.close();
}
Upvotes: 74