首页 > Web开发 > 详细

解析http协议的url

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!