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

Httpclient请求数据

时间:2016-05-02 18:33:10      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

package com.baidu.myutils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class MyUtils {
    
    String url="http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=json";

    
    public static String Check_j(String url){
        String str="";
        HttpGet get=new HttpGet(url);
        HttpParams params=new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        HttpConnectionParams.setSoTimeout(params, 5000);
        HttpClient client=new DefaultHttpClient(params);
        try {
            HttpResponse response=client.execute(get);
            
            
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity=response.getEntity();
                str=EntityUtils.toString(entity, "utf-8");
                
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
}

 

Httpclient请求数据

标签:

原文地址:http://www.cnblogs.com/1995yu/p/5452689.html

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