httpclient4 设置head

本文介绍如何使用Fiddler进行HTTP请求的拦截调试,并提供了一段Java代码示例,该示例展示了如何配置HTTP客户端以支持通过Fiddler代理进行请求拦截,并处理请求头中的Accept-Encoding等字段。
摘要由CSDN通过智能技术生成

请使用Fiddler拦截调试,


主要代码是

hc.addRequestInterceptor(myRequestAcceptEncoding);

 

详细代码如下


/**
* Get请求
@param url
@param params
@return
*/
public static String getsslDetail(String url, List<NameValuePair> params,List<NameValuePair> headMaps, String charset, Integer connectionTimeout, Integer soTimeout) {
DefaultHttpClient hc = new DefaultHttpClient();

if(isProxyByFiddler){
HttpHost proxy = new HttpHost("127.0.0.1",8888);
hc.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

}

hc = (DefaultHttpClient)wrapClient(hc);
String body = null;
try {
// Get请求
HttpGet httpget = new HttpGet(url);

String linkStr ="?";
int parmIndex =url.indexOf(linkStr);
if(parmIndex!=-1){
linkStr="&";
}
// 设置参数
//String str = EntityUtils.toString(new UrlEncodedFormEntity(params))
// 设置参数
if(params!=null && params.size()>0){
String str = EntityUtils.toString(new UrlEncodedFormEntity(params),charset);
url =httpget.getURI().toString() + linkStr + str;
//httpget.setURI(new URI(httpget.getURI().toString() + "?" + str))
httpget.setURI(new URI(url));
}

MyRequestAcceptEncoding myRequestAcceptEncoding = null;
if(headMaps!=null && headMaps.size()>0){
for (int i = 0; i < headMaps.size(); i++) {
String headName = headMaps.get(i).getName();
String headValue = headMaps.get(i).getValue();
String[] genHeadArr = {"User-Agent","Accept-Encoding","Accept","Connection"};
if(MyLangTools.contains(genHeadArr,headName)){
if("User-Agent".equals(headName)){
HttpProtocolParams.setUserAgent( hc.getParams(),headValue);
}
if("Accept-Encoding".equals(headName)){
if(myRequestAcceptEncoding == null){
myRequestAcceptEncoding = new MyRequestAcceptEncoding();
}
myRequestAcceptEncoding.setAcceptEncodingValue(headValue);
}
if("Accept".equals(headName)){
if(myRequestAcceptEncoding == null){
myRequestAcceptEncoding = new MyRequestAcceptEncoding();
}
myRequestAcceptEncoding.setAcceptValue(headValue);
}
if("Connection".equals(headName)){
if(myRequestAcceptEncoding == null){
myRequestAcceptEncoding = new MyRequestAcceptEncoding();
}
myRequestAcceptEncoding.setConnectionValue(headValue);
}
}else {
httpget.addHeader(headName, headValue);
}
}
if(myRequestAcceptEncoding !=null){
hc.addRequestInterceptor(myRequestAcceptEncoding);
}

}


// 设置参数
if(connectionTimeout!=null){
HttpConnectionParams.setConnectionTimeout(httpget.getParams(), connectionTimeout);
}
if(soTimeout!=null){
HttpConnectionParams.setSoTimeout(httpget.getParams(), soTimeout);
}


// 发送请求
HttpResponse httpresponse = hc.execute(httpget);

// 获取返回数据
HttpEntity entity = httpresponse.getEntity();

Header[] headers = httpresponse.getHeaders("Content-Encoding");
boolean isGzip = false;
for(Header h:headers){
if(h.getValue().equals("gzip")){
//返回头中含有gzip
isGzip = true;
}
}
String responseString = null;
if(isGzip){
//需要进行gzip解压处理
body = EntityUtils.toString(new GzipDecompressingEntity(entity));
}else{
body = EntityUtils.toString(entity);
}

EntityUtils.consume(entity);
catch (ParseException e) {
e.printStackTrace();
catch (UnsupportedEncodingException e) {
e.printStackTrace();
catch (IOException e) {
e.printStackTrace();
catch (URISyntaxException e) {
e.printStackTrace();
}
return body;
}


/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.base.httpclient4.parm;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.annotation.Immutable;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

/**
* Class responsible for handling Content Encoding requests in HTTP.
* <p>
* Instances of this class are stateless, therefore they're thread-safe and immutable.
*
@see "http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5"
*
@since 4.1
*/
@Immutable
public class MyRequestAcceptEncoding implements HttpRequestInterceptor {
private String acceptEncodingValue ;
private String acceptValue;
private String connectionValue;

public MyRequestAcceptEncoding(String acceptEncodingValue) {
this.acceptEncodingValue = acceptEncodingValue;
}

public MyRequestAcceptEncoding() {
}

/**
* Adds the header {@code "Accept-Encoding: gzip,deflate"} to the request.
*/
@Override
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {

/* Signal support for Accept-Encoding transfer encodings. */
if (!request.containsHeader("Accept-Encoding")) {
//request.addHeader("Accept-Encoding", "gzip,deflate");
request.addHeader("Accept-Encoding"acceptEncodingValue);
}
if (acceptValue!=null && !"".equals(acceptValue)) {
request.addHeader("Accept"acceptValue);
}
if (connectionValue!=null && !"".equals(connectionValue)) {
request.setHeader("Connection"connectionValue);
}

}

public String getAcceptEncodingValue() {
return acceptEncodingValue;
}

public void setAcceptEncodingValue(String acceptEncodingValue) {
this.acceptEncodingValue = acceptEncodingValue;
}

public String getAcceptValue() {
return acceptValue;
}

public void setAcceptValue(String acceptValue) {
this.acceptValue = acceptValue;
}

public String getConnectionValue() {
return connectionValue;
}

public void setConnectionValue(String connectionValue) {
this.connectionValue = connectionValue;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值