本文实例讲述了JAVA获取HTTP请求头的方法。分享给大家供大家参考,具体如下:
在利用Java网络编程时,利用Java获取HTTP Request 和 Response头字段;
可以利用Java语言根据需要添加自定义的HTTP头字段,而不必拘泥于标准HTTP定义的头字段。
代码如下:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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.junit.Test;
import java.util.*;
@Slf4j
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest
public class Test {
@Test
public void testRegister(){
// public static void main(String[] args) throws Exception {
String url = "localhost:8080/user/register";
// 请求路径
HttpPost httpPost=new HttpPost(url);
// 设置请求头
httpPost.addHeader("content-type", "application/json;chartset=UTF-8");
httpPost.addHeader("X-USER-AUTH","USERID=123456");
// 设置请求体
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair Header = new BasicNameValuePair("Header", "");
BasicNameValuePair Body = new BasicNameValuePair("Body", "");
list.add(Header);
list.add(Body);
// 使用URL实体转换工具
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entityParam);
// 请求连接配置
RequestConfig config = RequestConfig.custom()
// 连接请求超时ms
.setConnectTimeout(1000)
// 连接超时时间ms
.setConnectionRequestTimeout(1000)
// 读取超时ms
.setSocketTimeout(10 * 1000).build();
// 设置连接配置
httpPost.setConfig(config);
try {
// 发起请求
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpPost);
// 获取httpClient响应的请求内容entity
HttpEntity responseEntity = response.getEntity();
System.out.println("接口返回参数::"+
JSON.parseObject(EntityUtils.toString(responseEntity, "UTF-8")));
// 将返回体的信息转换为字符串
String mes = EntityUtils.toString(response.getEntity());
System.out.println("接口返回参数:"+mes);
// 获取httpClient响应的请求头header
Header responseHeader = response.getFirstHeader("X-USER-AUTH");
HeaderElement[] responseHeaderElements = responseHeader.getElements();
String phone = "";
String sessionId = "";
for (int i=0; i<responseHeaderElements.length; i++){
if ("PHONE".equals(responseHeaderElements[i].getName())){
phone = responseHeaderElements[i].getValue();
}
if ("SESSIONID".equals(responseHeaderElements[i].getName())){
sessionId = responseHeaderElements[i].getValue();
}
}
response.setHeader(responseHeader.getName(), responseHeader.getValue());
System.out.println("sessionId:"+sessionId);
System.out.println("phone:"+phone);
} catch (Exception e) {
e.printStackTrace();
}
}
}
参考:
JAVA获取HTTP请求头的方法示例:https://www.jb51.cc/java/510781.html
HttpClient发起请求,将响应结果(header和entity)设置到response中返回:https://www.cnblogs.com/yadongliang/p/13653323.html
Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header:https://blog.csdn.net/rosanu_blog/article/details/6934855