zoukankan      html  css  js  c++  java
  • httpclient开启代理,获取java中请求的url

    背景:在httpclent做post或者get请求时,请求返回的数据总是和预想的不一致,但是有不知道怎么排查问题,经同事说httpclient可以设置代理,就可以获取请求前数据的一些问题,帮助我排查问题,使用代理很方便,就3句话

    实现方式

    /**
    	 * 
    	 * @测试点: 发送get请求,并且设置cookies
    	 * @验证点: TODO(这里用一句话描述这个方法的作用)
    	 * @param @param
    	 *            url
    	 * @param @param
    	 *            cookies @备注: void
    	 * @author zhangjun
    	 * @date 2017年11月7日
    	 * @修改说明
    	 */
    	public String httpGet(String url, String cookies) {
    
    		String result = null;
    		
    		HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); //添加代理,IP为本地IP 8888就是fillder的端口
    		CloseableHttpClient httpClient = HttpClients.createDefault();//添加代理
    		try {
    			// 创建httpGet
    			HttpGet httpGet = new HttpGet(url);
    			System.out.println("获取的url为:"+url);
    			httpGet.setHeader("Connection", "keep-alive");
    			httpGet.addHeader(new BasicHeader("Cookie", cookies));
    
    			//代理
    			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    			httpGet.setConfig(config);
    			
    			System.out.println("executing request:" + httpGet.getURI());
    			// 执行get请求
    			CloseableHttpResponse response = httpClient.execute(httpGet);
    			try {
    				int code = response.getStatusLine().getStatusCode();
    				System.out.println("返回的状态码:" + code);
    				// 获取响应实体
    				HttpEntity entity = response.getEntity();
    				// 打印响应状态
    				System.out.println(response.getStatusLine());
    				if (entity != null) {
    					System.out.println("Response content length" + entity.getContentLength());
    					// 打印响应内容
    					result = EntityUtils.toString(entity);
    					// 打印响应头
    					System.out.println("Response content" + entity.getContent());
    					System.out.println("Response Contentype" + entity.getContentType());
    					System.out.println("Response ContenEncoding" + entity.getContentEncoding());
    				}
    				System.out.println("--------------");
    				Header[] hr = response.getAllHeaders();
    				for (int i = 0; i < hr.length; i++) {
    					Header header1 = hr[i];
    					System.out.println("头部内容:" + header1);
    				}
    
    			} finally {
    				response.close();
    			}
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			// 关闭连接,释放资源
    			try {
    				httpClient.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return result;
    	}
    

      

  • 相关阅读:
    ACM ICPC 2008–2009 NEERC MSC A, B, C, G, L
    POJ 1088 滑雪 DP
    UVA 11584 最短回文串划分 DP
    POJ 2531 Network Saboteur DFS+剪枝
    UVa 10739 String to Palindrome 字符串dp
    UVa 11151 Longest Palindrome 字符串dp
    UVa 10154 Weights and Measures dp 降维
    UVa 10271 Chopsticks dp
    UVa 10617 Again Palindrome 字符串dp
    UVa 10651 Pebble Solitaire 状态压缩 dp
  • 原文地址:https://www.cnblogs.com/chongyou/p/7808035.html
Copyright © 2011-2022 走看看