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;
}
}