您的位置:首页 > 理论基础 > 计算机网络

java http请求 获取xml格式

2014-03-25 15:14 295 查看
 /**
    * 向指定URL发送GET方法的请求
    * @param param
    *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
    */
   public static String sendGet(String url, String param) {
    HttpClient httpclient = new DefaultHttpClient();  
HttpGet httpgets = new HttpGet(url);    
HttpResponse response;
String result="";
try {
response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent(); 
result = convertStreamToString(instreams);
httpgets.abort();    
}
} catch (IOException e) {
e.printStackTrace();

return result;
   }
   
   public static String convertStreamToString(InputStream  is){
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;   
    try {
    while ((line = reader.readLine()) != null) {
    sb.append(line);   
    } 
    } catch (IOException e) {  
    e.printStackTrace(); 
     
    }finally{
    try{
    if(is!=null){
    is.close();
    }
    }
    catch(IOException ex){
    ex.printStackTrace();
    }

        }
    return sb.toString();   
   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java