JAVA: httpclient 详解——第一章;

 

JAVA: httpclient 详解——第一章;

分类: JAVA   352人阅读  评论(0)  收藏  举报


httpclient 详解——第一章;

httpclient 详解——第二章;

httpclient 详解——第三章;

httpclient 详解——第四章; 

httpclient 详解——第五章;

httpclient 详解——第六章;

httpclient 详解——第七章;


相对于httpurlconnection ,httpclient更加丰富,也更加强大,其中apache有两个项目都是httpclient,一个是commonts包下的,这个是通用的,更专业的是org.apache.http.包下的,所以我一般用后者;


httpclient可以处理长连接,保存会话,重连接,以及请求过滤器,连接重用等等...


下面是测试代码(全部总结来自官方文档,以及翻译)


须要下载核心包:httpclient-4.3.4.jar ,也可在官网下载:http://hc.apache.org/downloads.cgi



[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package httpClientTest;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.InterruptedIOException;  
  7. import java.net.URI;  
  8. import java.net.URISyntaxException;  
  9. import java.net.UnknownHostException;  
  10. import java.nio.charset.Charset;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13. import java.util.concurrent.ExecutionException;  
  14. import java.util.concurrent.TimeUnit;  
  15. import java.util.concurrent.atomic.AtomicInteger;  
  16.   
  17. import javax.net.ssl.SSLException;  
  18.   
  19. import org.apache.http.Consts;  
  20. import org.apache.http.HeaderElement;  
  21. import org.apache.http.HeaderElementIterator;  
  22. import org.apache.http.HttpClientConnection;  
  23. import org.apache.http.HttpEntity;  
  24. import org.apache.http.HttpEntityEnclosingRequest;  
  25. import org.apache.http.HttpException;  
  26. import org.apache.http.HttpHost;  
  27. import org.apache.http.HttpRequest;  
  28. import org.apache.http.HttpRequestInterceptor;  
  29. import org.apache.http.HttpResponse;  
  30. import org.apache.http.NameValuePair;  
  31. import org.apache.http.client.ClientProtocolException;  
  32. import org.apache.http.client.HttpRequestRetryHandler;  
  33. import org.apache.http.client.ResponseHandler;  
  34. import org.apache.http.client.config.RequestConfig;  
  35. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  36. import org.apache.http.client.methods.CloseableHttpResponse;  
  37. import org.apache.http.client.methods.HttpGet;  
  38. import org.apache.http.client.methods.HttpPost;  
  39. import org.apache.http.client.protocol.HttpClientContext;  
  40. import org.apache.http.client.utils.URIBuilder;  
  41. import org.apache.http.client.utils.URIUtils;  
  42. import org.apache.http.conn.ConnectTimeoutException;  
  43. import org.apache.http.conn.ConnectionKeepAliveStrategy;  
  44. import org.apache.http.conn.ConnectionRequest;  
  45. import org.apache.http.conn.HttpClientConnectionManager;  
  46. import org.apache.http.conn.routing.HttpRoute;  
  47. import org.apache.http.entity.ContentType;  
  48. import org.apache.http.impl.client.CloseableHttpClient;  
  49. import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;  
  50. import org.apache.http.impl.client.HttpClients;  
  51. import org.apache.http.impl.client.LaxRedirectStrategy;  
  52. import org.apache.http.impl.conn.BasicHttpClientConnectionManager;  
  53. import org.apache.http.message.BasicHeaderElementIterator;  
  54. import org.apache.http.message.BasicNameValuePair;  
  55. import org.apache.http.protocol.HTTP;  
  56. import org.apache.http.protocol.HttpContext;  
  57. import org.apache.http.util.EntityUtils;  
  58.   
  59. public class Main {  
  60.   
  61.       
  62.     public static void main(String[] args) {  
  63.           
  64.         try {  
  65.             test9();  
  66.         } catch (Exception e) {  
  67.             // TODO Auto-generated catch block  
  68.             e.printStackTrace();  
  69.         }  
  70.     }  
  71.       
  72.       
  73.       
  74.       
  75.       
  76.       
  77.     /** 
  78.      * 测试1: 构建复杂uri,这种方式会很方便的设置多个参数; 
  79.      *  
  80.      * HttpClients类是client的具体一个实现类; 
  81.      *  
  82.      * URIBuilder包含:协议,主机名,端口(可选),资源路径,和多个参数(可选) 
  83.      *  
  84.      */  
  85.     private static void test1() {  
  86.           
  87.         CloseableHttpClient client = HttpClients.createDefault();  
  88.           
  89.         URI uri = null;  
  90.         try {  
  91.             uri = new URIBuilder()  
  92.             .setScheme("http")  
  93.             .setHost("webservice.webxml.com.cn")  
  94.             .setPath("/WebServices/MobileCodeWS.asmx/getDatabaseInfo")  
  95.             .setParameter("""")//这里可以设置多个参数  
  96.             .setParameter("""")  
  97.             .setParameter("""")  
  98.             .setParameter("""")  
  99.             .build();  
  100.         } catch (URISyntaxException e1) {  
  101.             e1.printStackTrace();  
  102.         }  
  103.           
  104.         //http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo  
  105.         HttpGet get = new HttpGet(uri);  
  106.         try {  
  107.             CloseableHttpResponse response = client.execute(get);  
  108.               
  109.             if(response.getStatusLine().getStatusCode()==200){  
  110.                   
  111.                   
  112.                 System.out.println(EntityUtils.toString(response.getEntity()));  
  113.                   
  114.                 //以下这种方式读取流也可以,只不过用EntityUtils会更方便  
  115.                 /*InputStream is = response.getEntity().getContent(); 
  116.                 ByteArrayOutputStream os = new ByteArrayOutputStream(); 
  117.                 byte[] buffer = new byte[1024]; 
  118.                 int len=-1; 
  119.                 while((len = is.read(buffer))!=-1){ 
  120.                     os.write(buffer,0,len); 
  121.                 } 
  122.                 os.close(); 
  123.                 is.close(); 
  124.                 System.out.println(os.size()+new String(os.toByteArray(),"utf-8"));*/  
  125.             }  
  126.         } catch (ClientProtocolException e) {  
  127.             // TODO Auto-generated catch block  
  128.             e.printStackTrace();  
  129.         } catch (IOException e) {  
  130.             // TODO Auto-generated catch block  
  131.             e.printStackTrace();  
  132.         }  
  133.     }  
  134.       
  135.       
  136.     /** 
  137.      * 为uri进行加密,并进行表单提交; 
  138.      *  
  139.      * 许多应用需要模拟提交的HTML表单的处理,例如,在 
  140.         为了登录到Web应用程序或提交的输入数据。 HttpClient提供的实体类 
  141.         UrlEncodedFormEntity可以实现该过程; 
  142.      */  
  143.     private static void test2(){  
  144.           
  145.         CloseableHttpClient client = HttpClients.createDefault();  
  146.           
  147.         HttpPost httppost = new HttpPost("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");  
  148.           
  149.         //构建请求参数  
  150.         List<NameValuePair> list = new ArrayList<NameValuePair>();  
  151.         list.add(new BasicNameValuePair("mobileCode""110"));  
  152.         list.add(new BasicNameValuePair("userID"""));  
  153.   
  154.         //构建url加密实体,并以utf-8方式进行加密;  
  155.         UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);  
  156.         httppost.setEntity(entity);  
  157.           
  158.         try {  
  159.             CloseableHttpResponse response = client.execute(httppost);  
  160.               
  161.             if(response.getStatusLine().getStatusCode()==200){  
  162.                   
  163.                 //org.apache.http.util.EntityUtils类可以快速处理服务器返回实体对象  
  164.                 System.out.println(EntityUtils.toString(response.getEntity()));  
  165.                   
  166.             }  
  167.         } catch (ClientProtocolException e) {  
  168.             // TODO Auto-generated catch block  
  169.             e.printStackTrace();  
  170.         } catch (IOException e) {  
  171.             // TODO Auto-generated catch block  
  172.             e.printStackTrace();  
  173.         }  
  174.       
  175.     }  
  176.       
  177.       
  178.     /** 
  179.      * 以回调方式处理返回结果 
  180.      *  
  181.      *  处理响应的最简单和最方便的方法是通过使用ResponseHandler的 
  182.         接口。用户不必担心连接管理的问题。当使用一个 
  183.         ResponseHandler的时候,无论是否请求执行成功或导致异常,HttpClient将会自动释放连接。 
  184.      */  
  185.     private static void test3(){  
  186.           
  187.         CloseableHttpClient client = HttpClients.createDefault();  
  188.           
  189.         //==============================================================  
  190.         ResponseHandler<Object> handler = new ResponseHandler<Object>() {  
  191.             @Override  
  192.             public Object handleResponse(final HttpResponse response) throws IOException {  
  193.                   
  194.                 HttpEntity entity = response.getEntity();  
  195.                   
  196.                 if (entity == null) {  
  197.                     throw new ClientProtocolException("返回结果为空");  
  198.                 }  
  199.               
  200.                 if(response.getStatusLine().getStatusCode()==200){  
  201.                       
  202.                     //获取返回结果的字符集 如:utf-8  gbk,并以这种字符集来读取流信息  
  203.                     ContentType contentType = ContentType.getOrDefault(entity);  
  204.                     Charset charset = contentType.getCharset();  
  205.                       
  206.                     InputStreamReader reader = new InputStreamReader(entity.getContent(), charset);  
  207.                     BufferedReader br = new BufferedReader(reader);  
  208.                     StringBuffer sb = new StringBuffer();  
  209.                     char[] buffer = new char[1024];  
  210.                     while (br.read(buffer)!=-1) {  
  211.                         sb.append(new String(buffer));  
  212.                     }  
  213.                       
  214.                     return sb.toString();  
  215.                 }  
  216.                   
  217.                 return null;  
  218.                   
  219.             }  
  220.         };  
  221.         //===================================================================  
  222.           
  223.         URI uri = null;//构建uri实体  
  224.         try {  
  225.             uri = new URIBuilder()  
  226.             .setScheme("http")  
  227.             .setHost("webservice.webxml.com.cn")  
  228.             .setPath("/WebServices/MobileCodeWS.asmx/getDatabaseInfo")  
  229.             .setParameter("""")  
  230.             .setParameter("""")  
  231.             .setParameter("""")  
  232.             .build();  
  233.               
  234.         } catch (URISyntaxException e) {  
  235.             e.printStackTrace();  
  236.         }  
  237.           
  238.         HttpPost post = new HttpPost(uri);  
  239.           
  240.         try {  
  241.               
  242.             //handler回调  
  243.              Object obj = client.execute(post, handler);  
  244.                
  245.              System.out.println("返回结果:"+obj);  
  246.               
  247.         } catch (ClientProtocolException e) {  
  248.             e.printStackTrace();  
  249.         } catch (IOException e) {  
  250.             e.printStackTrace();  
  251.         }  
  252.           
  253.     }  
  254.       
  255.       
  256.       
  257.     /** 
  258.      *  设置长连接策略,根据服务器的约束或者客户端的约束来设置长连接的时长; 
  259.      */  
  260.     private static void test4() {  
  261.           
  262.         ConnectionKeepAliveStrategy strategy = new DefaultConnectionKeepAliveStrategy() {  
  263.               
  264.             /** 
  265.              * 服务器端配置(以tomcat为例):keepAliveTimeout=60000,表示在60s内内,服务器会一直保持连接状态。 
  266.              * 也就是说,如果客户端一直请求服务器,且间隔未超过60s,则该连接将一直保持,如果60s内未请求,则超时。 
  267.              *  
  268.              * getKeepAliveDuration返回超时时间; 
  269.              */  
  270.             @Override  
  271.             public long getKeepAliveDuration(HttpResponse response, HttpContext context) {  
  272.                   
  273.                   
  274.                 //如果服务器指定了超时时间,则以服务器的超时时间为准  
  275.                 HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));  
  276.                 while (it.hasNext()) {  
  277.                     HeaderElement he = it.nextElement();  
  278.                     String param = he.getName();  
  279.                     String value = he.getValue();  
  280.                     if (value != null && param.equalsIgnoreCase("timeout")) {  
  281.                         try {  
  282.                             System.out.println("服务器指定的超时时间:" + value + " 秒");  
  283.                             return Long.parseLong(value) * 1000;  
  284.                         } catch (NumberFormatException ignore) {  
  285.                             ignore.printStackTrace();  
  286.                         }  
  287.                     }  
  288.                 }  
  289.                   
  290.                   
  291.                 long keepAlive = super.getKeepAliveDuration(response, context);  
  292.                   
  293.                  // 如果服务器未指定超时时间,则客户端默认30s超时  
  294.                 if (keepAlive == -1) {  
  295.                     keepAlive = 30 * 1000;  
  296.                 }  
  297.                   
  298.                 return keepAlive;  
  299.                   
  300.                 /*如果访问webservice.webxml.com.cn主机,则超时时间5秒,其他主机超时时间30秒 
  301.                 HttpHost host = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); 
  302.                 if ("webservice.webxml.com.cn".equalsIgnoreCase(host.getHostName())) { 
  303.                     keepAlive =  10 * 1000; 
  304.                 } else { 
  305.                     keepAlive =  30 * 1000; 
  306.                 }*/  
  307.                   
  308.                   
  309.             }  
  310.         };  
  311.           
  312.           
  313.           
  314.         CloseableHttpClient client = HttpClients.custom().setKeepAliveStrategy(strategy).build();  
  315.           
  316.         URI uri = null;//构建uri实体  
  317.         try {  
  318.             uri = new URIBuilder()  
  319.             .setScheme("http")  
  320.             .setHost("webservice.webxml.com.cn")  
  321.             .setPath("/WebServices/MobileCodeWS.asmx/getDatabaseInfo")  
  322.             .setParameter("""")  
  323.             .build();  
  324.               
  325.         } catch (URISyntaxException e) {  
  326.             e.printStackTrace();  
  327.         }  
  328.           
  329.         HttpPost post = new HttpPost(uri);  
  330.           
  331.         try {  
  332.               
  333.             CloseableHttpResponse response =  client.execute(post);  
  334.             if(response.getStatusLine().getStatusCode()==200){  
  335.                 String result = EntityUtils.toString(response.getEntity());  
  336.                 System.out.println("返回的结果====="+result);  
  337.             }  
  338.               
  339.         } catch (ClientProtocolException e) {  
  340.             e.printStackTrace();  
  341.         } catch (IOException e) {  
  342.             e.printStackTrace();  
  343.         }  
  344.           
  345.       
  346.           
  347.     }  
  348.       
  349.       
  350.     /** 
  351.      * 构建复杂请求体 
  352.      *  
  353.      * RequestConfig将会保存在context上下文中,并会在连续的请求中进行传播(来自官方文档); 
  354.      *  
  355.      */  
  356.     private void test5(){  
  357.   
  358.           
  359.         CloseableHttpClient client = HttpClients.createDefault();  
  360.           
  361.         //构建请求配置  
  362.         RequestConfig config = RequestConfig.  
  363.                 custom().  
  364.                 setSocketTimeout(1000*10).  
  365.                 setConnectTimeout(1000*10).  
  366.                 build();  
  367.         //==============================================================  
  368.         ResponseHandler<Object> handler = new ResponseHandler<Object>() {//回调  
  369.             @Override  
  370.             public Object handleResponse(final HttpResponse response) throws IOException {  
  371.                   
  372.                 HttpEntity entity = response.getEntity();  
  373.                   
  374.                 if (entity == null) {  
  375.                     throw new ClientProtocolException( "返回结果为空");  
  376.                 }  
  377.               
  378.                 if(response.getStatusLine().getStatusCode()==200){  
  379.                     ContentType contentType = ContentType.getOrDefault(entity);  
  380.                     Charset charset = contentType.getCharset();  
  381.                       
  382.                     InputStreamReader reader = new InputStreamReader(entity.getContent(), charset);  
  383.                     BufferedReader br = new BufferedReader(reader);  
  384.                     StringBuffer sb = new StringBuffer();  
  385.                     char[] buffer = new char[1024];  
  386.                     while (br.read(buffer)!=-1) {  
  387.                         sb.append(new String(buffer));  
  388.                     }  
  389.                       
  390.                     return sb.toString();  
  391.                 }  
  392.                   
  393.                 return null;  
  394.                   
  395.             }  
  396.         };  
  397.         //===================================================================  
  398.           
  399.         URI uri = null;  
  400.         try {  
  401.             uri = new URIBuilder()  
  402.             .setScheme("http")  
  403.             .setHost("webservice.webxml.com.cn")  
  404.             .setPath("/WebServices/MobileCodeWS.asmx/getDatabaseInfo")  
  405.             .setParameter("""")  
  406.             .setParameter("""")  
  407.             .setParameter("""")  
  408.             .build();  
  409.               
  410.         } catch (URISyntaxException e) {  
  411.             e.printStackTrace();  
  412.         }  
  413.           
  414.         HttpPost post = new HttpPost(uri);  
  415.           
  416.         post.setConfig(config);//设置请求配置  
  417.           
  418.         try {  
  419.              Object obj = client.execute(post, handler);  
  420.                
  421.              System.out.println("返回结果:"+obj);  
  422.               
  423.         } catch (ClientProtocolException e) {  
  424.             e.printStackTrace();  
  425.         } catch (IOException e) {  
  426.             e.printStackTrace();  
  427.         }  
  428.           
  429.       
  430.     }  
  431.       
  432.       
  433.       
  434.     /** 
  435.      *  异常恢复机制: 
  436.      *   
  437.      *  HttpRequestRetryHandler连接失败后,可以针对相应的异常进行相应的处理措施; 
  438.      *  HttpRequestRetryHandler接口须要用户自己实现; 
  439.      *   
  440.      */  
  441.     private static  void test6(){  
  442.   
  443.         HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {  
  444.               
  445.             /** 
  446.              * exception异常信息; 
  447.              * executionCount:重连次数; 
  448.              * context:上下文 
  449.              */  
  450.             @Override  
  451.             public boolean retryRequest(IOException exception, int executionCount,HttpContext context) {  
  452.   
  453.                 System.out.println("重连接次数:"+executionCount);  
  454.                   
  455.                 if (executionCount >= 5) {//如果连接次数超过5次,就不进行重复连接  
  456.                     return false;  
  457.                 }  
  458.                 if (exception instanceof InterruptedIOException) {//io操作中断  
  459.                     return false;  
  460.                 }  
  461.                 if (exception instanceof UnknownHostException) {//未找到主机  
  462.                     // Unknown host  
  463.                     return false;  
  464.                 }  
  465.                 if (exception instanceof ConnectTimeoutException) {//连接超时  
  466.                     return true;  
  467.                 }  
  468.                 if (exception instanceof SSLException) {  
  469.                     // SSL handshake exception  
  470.                     return false;  
  471.                 }  
  472.                 HttpClientContext clientContext = HttpClientContext.adapt(context);  
  473.                   
  474.                 HttpRequest request = clientContext.getRequest();  
  475.                   
  476.                 boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);  
  477.                   
  478.                 if (idempotent) {  
  479.                     // Retry if the request is considered idempotent  
  480.                     return true;  
  481.                 }  
  482.                 return false;  
  483.             }  
  484.         };  
  485.           
  486.           
  487.         CloseableHttpClient client = HttpClients.custom().setRetryHandler(retryHandler).build();  
  488.           
  489.         RequestConfig config = RequestConfig.  
  490.                 custom().  
  491.                 setSocketTimeout(1000*10).  
  492.                 setConnectTimeout(1000*10).  
  493.                 build();  
  494.           
  495.         HttpPost post = new HttpPost("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
  496.           
  497.         post.setConfig(config);  
  498.           
  499.         try {  
  500.             HttpResponse response = client.execute(post);  
  501.             HttpEntity entity = response.getEntity();  
  502.               
  503.             ContentType contentType = ContentType.getOrDefault(entity);  
  504.             Charset charset = contentType.getCharset();  
  505.   
  506.             InputStreamReader reader = new InputStreamReader(entity.getContent(), charset);  
  507.             BufferedReader br = new BufferedReader(reader);  
  508.             StringBuffer sb = new StringBuffer();  
  509.             char[] buffer = new char[1024];  
  510.             while (br.read(buffer) != -1) {  
  511.                 sb.append(new String(buffer));  
  512.             }  
  513.   
  514.             System.out.println("返回的结果:"+sb.toString());  
  515.   
  516.         } catch (ClientProtocolException e) {  
  517.             e.printStackTrace();  
  518.         } catch (IOException e) {  
  519.             e.printStackTrace();  
  520.         }  
  521.           
  522.     }  
  523.       
  524.       
  525.     /** 
  526.      *  HTTP请求过滤器,在执行请求之前拦截HttpRequest 和 HttpContext; 
  527.      */  
  528.     private static void test7() throws ClientProtocolException, IOException{  
  529.           
  530.         HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {  
  531.             /** 
  532.              * 处理请求: 
  533.              * 如果是客户端:这个处理在发送请求之前执行; 
  534.              * 如果是服务器:这个处理在接收到请求体之前执行; 
  535.              */  
  536.             public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {  
  537.                 AtomicInteger count = (AtomicInteger) context.getAttribute("count");  
  538.                 request.addHeader("count", Integer.toString(count.getAndIncrement()));  
  539.             }  
  540.         };  
  541.           
  542.           
  543.         CloseableHttpClient httpclient = HttpClients.  
  544.                         custom().  
  545.                         addInterceptorLast(requestInterceptor).  
  546.                         build();  
  547.           
  548.           
  549.         AtomicInteger count = new AtomicInteger(1);  
  550.         HttpClientContext context = HttpClientContext.create();  
  551.         context.setAttribute("count", count);  
  552.         HttpGet httpget = new HttpGet("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
  553.           
  554.         for (int i = 0; i < 10; i++) {  
  555.             CloseableHttpResponse response = httpclient.execute(httpget,context);  
  556.             try {  
  557.                   
  558.                 HttpEntity entity = response.getEntity();  
  559. //              System.out.println(EntityUtils.toString(entity));  
  560.                   
  561.             } finally {  
  562.                 response.close();  
  563.             }  
  564.         }  
  565.     }  
  566.       
  567.       
  568.       
  569.       
  570.     /** 
  571.      *  
  572.      *  httpclient会自动处理重定向;  
  573.      *   
  574.      *  301 永久重定向,告诉客户端以后应从新地址访问. 
  575.         302 作为HTTP1.0的标准,以前叫做Moved Temporarily ,现在叫Found. 现在使用只是为了兼容性的  处理,包括PHP的默认Location重定向用的也是302. 
  576.         但是HTTP 1.1 有303 和307作为详细的补充,其实是对302的细化 
  577.         303:对于POST请求,它表示请求已经被处理,客户端可以接着使用GET方法去请求Location里的URI。 
  578.         307:对于POST请求,表示请求还没有被处理,客户端应该向Location里的URI重新发起POST请求。 
  579.      */  
  580.     private static void test8() throws ClientProtocolException, IOException,  
  581.             URISyntaxException {  
  582.   
  583.         LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();  
  584.         CloseableHttpClient httpclient = HttpClients.custom().setRedirectStrategy(redirectStrategy).build();  
  585.   
  586.         HttpClientContext context = HttpClientContext.create();  
  587.         HttpGet httpget = new HttpGet("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo");  
  588.         CloseableHttpResponse response = httpclient.execute(httpget, context);  
  589.         try {  
  590.             HttpHost target = context.getTargetHost();  
  591.             List<URI> redirectLocations = context.getRedirectLocations();  
  592.             URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);  
  593.             System.out.println("Final HTTP location: " + location.toASCIIString());  
  594.         } finally {  
  595.             response.close();  
  596.         }  
  597.     }  
  598.       
  599.       
  600.       
  601.       
  602. }  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值