码迷,mamicode.com
首页 > Web开发 > 详细

解析http协议的url

时间:2017-04-27 23:21:00      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:puts   ati   url   except   app   ret   code   ack   amr   

package util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ServerUtil{
  public static StringBuilder getServerResponse(HttpURLConnection connection){
    String strLine = "";
    StringBuilder strResponse = new StringBuilder();
    try{
      Map<String,List<String>> rspHeaders = connection.getHeaderFields();
      Set<String> rspHeadNames = rspHeaders.keySet();
      for (String key : rspHeadNames) {
        if(null!=key){
          strResponse.append(key+":");
        }
        strResponse.append(new String(rspHeaders.get(key).get(0).getBytes("iso-8859-1"),"UTF-8")+"\n");
      }
      //获取状态码
      int code = connection.getResponseCode();
      InputStream in;
      if(code==200){
        in = connection.getInputStream();
      }else{
        in = connection.getErrorStream();
      }
      if(null!=in){
        //读取一串字符
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        while((strLine=reader.readLine())!=null){
          strResponse.append("\n"+strLine);
        }
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return strResponse;
  }
}

解析http协议的url

标签:puts   ati   url   except   app   ret   code   ack   amr   

原文地址:http://www.cnblogs.com/hxinbk/p/6777629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!