2017年8月7日 星期一

[ Java 常見問題 ] HttpComponent - How to find mimetype of response

Source From Here 
Question 
I am working with a GET request made using Apache HttpComponents (v4- the latest version; not the older v3)... How do I obtain the mimetype of the response? 

How-To 
A fully sample code as below: 
  1. package demo;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.net.URI;  
  6. import java.util.List;  
  7.   
  8. import org.apache.http.client.config.RequestConfig;  
  9. import org.apache.http.client.methods.CloseableHttpResponse;  
  10. import org.apache.http.client.methods.HttpGet;  
  11. import org.apache.http.client.protocol.HttpClientContext;  
  12. import org.apache.http.impl.client.CloseableHttpClient;  
  13. import org.apache.http.impl.client.HttpClients;  
  14.   
  15. public class GetDemo {  
  16.   
  17.     public static void main(String[] args) throws Exception {  
  18.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  19.         try {  
  20.             HttpClientContext context = HttpClientContext.create();  
  21.             RequestConfig requestConfig = RequestConfig.custom()  
  22.                     .setSocketTimeout(10000)  
  23.                     .setConnectTimeout(10000)  
  24.                     .build();  
  25.             HttpGet httpget1 = new HttpGet(args[0]);  
  26.             URI finalUrl = null;  
  27.             httpget1.setConfig(requestConfig);  
  28.             CloseableHttpResponse resp = httpclient.execute(httpget1, context);  
  29.             List locations = context.getRedirectLocations();  
  30.             if (locations != null) {  
  31.                 finalUrl = locations.get(locations.size() - 1);  
  32.             }             
  33.             int code = resp.getStatusLine().getStatusCode();  
  34.             String ct = resp.getEntity().getContentType().getValue();  // <--- get="" here="" mime="" span="" type="">  
  35.             System.out.printf("Status code=%d (Content-type=%s)\n", code, ct);  
  36.             if(finalUrl!=null)  
  37.             {  
  38.                 System.out.printf("%s\n", finalUrl);  
  39.             }  
  40.             else  
  41.             {  
  42.                 System.out.printf("%s\n", args[0]);  
  43.             }  
  44.             BufferedReader br = new BufferedReader(new InputStreamReader((resp.getEntity().getContent())));  
  45.             if(ct.trim().contains("text/html"))  
  46.             {  
  47.                 String line = null;  
  48.                 while((line=br.readLine())!=null)  
  49.                 {  
  50.                     System.out.printf("%s\n", line);                  
  51.                 }  
  52.             }  
  53.             System.out.printf("===== DONE =====\n");  
  54.             resp.close();  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.         } finally {  
  58.             httpclient.close();  
  59.         }  
  60.     }  
  61.   
  62. }  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...