import java.io.File;import org.apache.commons.io.IOUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.springframework.context.support.FileSystemXmlApplicationContext;public class ProxyHttpClient { public static void main(String[] args) throws Exception { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("file:" + new File("./ProxyHttpClient.xml").getCanonicalPath()); try { HttpClient client = context.getBean(HttpClient.class); HttpResponse response = client.execute(new HttpGet("http://cn.bing.com/")); String contentType = response.getFirstHeader("Content-Type").getValue(); System.out.println(IOUtils.toString(response.getEntity().getContent(), contentType.substring(contentType.lastIndexOf('=') + 1))); } finally { context.close(); } }}
如果要拦截https请求,那么需要把Fiddler的证书导入到信任证书库。