Java HttpURLConnection GET Request
For Java HTTP GET requests, we have all the information in the browser URL itself.
Example
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
public class HttpGet {
private HttpURLConnection httpConn;
private String charset;
// Set the connect timeout value in milliseconds
private final int CONNECT_TIMEOUT = 15000;
// Set the read timeout value in milliseconds
private final int READ_TIMEOUT = 60000;
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
*
* @param requestURL
* @param charset
* @param headers
* @throws IOException
*/
public HttpGet(String requestURL, String charset, Map<String, String> headers) throws IOException {
this.charset = charset;
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setConnectTimeout(CONNECT_TIMEOUT);
httpConn.setReadTimeout(READ_TIMEOUT);
httpConn.setRequestMethod("GET"); // Set method:get
if (headers != null && headers.size() > 0) {
Iterator<String> it = headers.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = headers.get(key);
httpConn.setRequestProperty(key, value);
}
}
}
public HttpGet(String requestURL, String charset) throws IOException {
this(requestURL, charset, null);
}
/**
* Adds a header to the request
*
* @param key
* @param value
*/
public void addHeader(String key, String value) {
httpConn.setRequestProperty(key, value);
}
/**
* Completes the request and receives response from the server.
*
* @return String as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public String finish() throws IOException {
String response = "";
// Check the http status
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = httpConn.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}
response = result.toString(this.charset);
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
}
Use it
For example, we need to query the information of the IP address. Crawling the website http://www.ip138.com, pass the ip address. The content encoding of this website is gbk.
try {
// Set the header
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
HttpGet httpGet = new HttpGet("http://www.ip138.com/iplookup.asp?ip=8.8.8.8&action=2", "gbk", headers);
// Get the result
String response = httpGet.finish();
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
When creating URLs, you may need to pass special symbols. So it is better to encode the parameters. Use the URLEncoder.encode(String s, String enc)
method. Specify an encoding, such as UTF-8 or gbk.
try {
StringBuilder sb = new StringBuilder();
sb.append(URLEncoder.encode("test", "UTF-8"));
sb.append("=");
sb.append(URLEncoder.encode("测试", "UTF-8"));
HttpGet multipart = new HttpGet("http://localhost?" + sb.toString(), "utf-8");
// Get the result
String response = multipart.finish();
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}