在云那方

首页 新随笔 联系 订阅 管理
    public String get(String url)
    {
        HttpClient client = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();
     
        HttpGet myget = new HttpGet(url);
        try 
        {
            HttpResponse response = client.execute(myget);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }
        } 
        catch (Exception e) 
        {
        	System.out.println("调用失败:" + e.getMessage());
        }
        
        return builder.toString();
    }
    
    public String post(String url, List <NameValuePair> params)
    {
    	String retSrc = null;
    	try 
    	{
    		HttpPost request = new HttpPost(url);  
    		request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 

        	// 发送请求  
        	HttpResponse httpResponse = new DefaultHttpClient().execute(request);  
        	retSrc = EntityUtils.toString(httpResponse.getEntity());  
		} 
    	catch (Exception e) 
		{
    		System.out.println("调用失败:" + e.getMessage());
		}
    	return retSrc;
    }

  

posted on 2013-03-11 11:05  Rich.T  阅读(178)  评论(0编辑  收藏  举报