Java中如何发起HTTP请求

=Start=

缘由:

之前在学习Java的时候就说要整理一篇如何进行HTTP操作的文章,不过一直都没有写好,这两天刚好想到了验证的方法,所以整个过程很快就OK了,在此记录一下,方便以后参考。

正文:

参考解答:
1、使用 HttpURLConnection
package com.ixyzero.learn.utils;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Created by ixyzero on 2019/5/11.
*/
public class HttpURLConnectionExample {

private final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception {

HttpURLConnectionExample http = new HttpURLConnectionExample();

System.out.println("Testing 1 - Send Http GET request");
http.sendGet();

System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();

}

// HTTP GET request
private void sendGet() throws Exception {

String url = "https://httpbin.org/get?q=test";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// optional default is GET
con.setRequestMethod("GET");

//add request header
con.setRequestProperty("User-Agent", USER_AGENT);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println(response.toString());
}

// HTTP POST request
private void sendPost() throws Exception {

String url = "https://httpbin.org/post";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "sn=123&id=12345";

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println(response.toString());
}

}
2、使用 HttpClient
package com.ixyzero.learn.utils;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by ixyzero.com on 2019/5/11.
*/
public class HttpUtils {

private static String getURL = "http://httpbin.org/get?q=test";
private static String postURL = "https://httpbin.org/post";
private static String USER_AGENT = "Mozilla/5.0";
private static int TIMEOUT = 50;

public String httpGet(String url) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.addHeader("User-Agent", USER_AGENT);
StringBuffer result = new StringBuffer();

try {
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}

return result.toString();
}

public String httpPost(String url, Map<String, String> mapParam) {
StringBuffer result = new StringBuffer();

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);

List<NameValuePair> urlParameters = new ArrayList<>();
for (Map.Entry<String, String> entry : mapParam.entrySet()) {
urlParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}

try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return result.toString();
}

public static void main(String[] args) {
HttpUtils httpUtils = new HttpUtils();
String response;

response = httpUtils.httpGet(getURL);
System.out.println(String.format("URL: %s\nResponse:\n%s\n", getURL, response));

Map<String, String> map = new HashMap<String, String>() {
{
put("name", "test");
put("age", "20");
}
};
System.out.println(map);
response = httpUtils.httpPost(postURL, map);
System.out.println(String.format("URL: %s\nResponse:\n%s\n", postURL, response));

}
}

&

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
3、借助 https://httpbin.org/验证效果

该网站会把你给它发出的请求信息打出来,方便你验证参数和设置是否正确,上面的POST信息如下(包含请求内容、请求头信息、外网IP等):

{
"args": {},
"data": "",
"files": {},
"form": {
"age": "20",
"name": "test"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Content-Length": "16",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0"
},
"json": null,
"origin": "x1.x2.x3.x4, x1.x2.x3.x4",
"url": "https://httpbin.org/post"
}
4、如何解析返回的json数据

在Java中处理json数据时,我一般用的是Alibaba的fastjson库,所以这里也用这个库的相关方法进行说明:

// 当response为json格式的字符串时,直接将其传给JSONObject.parseObject方法进行解析即可得到对应的json对象
JSONObject jsonObj = JSONObject.parseObject(response);
参考链接:

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/4409.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注