HttpClient的get和post方式提交数据的使用

简介: /** * Http工具类 */ public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new DefaultHttpClient(); public...
/**
 * Http工具类
 */
public class HttpUtil {
    // 创建HttpClient对象
    public static HttpClient httpClient = new DefaultHttpClient();
    public static final String BASE_URL = "";

    /**
     * get请求
     *
     * @param url
     *            发送请求的URL
     * @return 服务器响应字符串
     * @throws Exception
     */
    public static String doGet(String url) throws Exception {
        // 创建HttpGet对象。
        HttpGet get = new HttpGet(url);
        // 发送GET请求
        HttpResponse httpResponse = httpClient.execute(get);
        // 如果服务器成功地返回响应
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            // 获取服务器响应字符串
            HttpEntity entity = httpResponse.getEntity();
            InputStream content = entity.getContent();
            return convertStreamToString(content);
        }
        return null;
    }

    /**
     * post请求
     *
     * @param url
     *            发送请求的URL
     * @param params
     *            请求参数
     * @return 服务器响应字符串
     * @throws Exception
     */
    public static String doPost(String url, Map<String, String> rawParams)
            throws Exception {
        // 创建HttpPost对象。
        HttpPost post = new HttpPost(url);
        // 如果传递参数个数比较多的话可以对传递的参数进行封装
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (String key : rawParams.keySet()) {
            // 封装请求参数
            params.add(new BasicNameValuePair(key, rawParams.get(key)));
        }
        // 设置请求参数
        post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        // 发送POST请求
        HttpResponse httpResponse = httpClient.execute(post);
        // 如果服务器成功地返回响应
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            // 获取服务器响应字符串
            HttpEntity entity = httpResponse.getEntity();
            InputStream content = entity.getContent();
            return convertStreamToString(content);
        }
        return null;
    }

    /**
     * 获取服务器的响应,转换为字符串
     */
    private 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 {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

 

相关文章
|
JSON 数据格式
okhttp3 模拟get、post(json参数传递,form表单提交)
本文是博主学习okhttp3 的记录,希望对大家有所帮助。
1547 0
Kam
|
Java Maven
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Kam
1599 0
|
5月前
|
JSON 数据格式
OkHttp3发起POST或GET请求
OkHttp3发起POST或GET请求
127 0
|
10月前
|
Web App开发 网络协议 安全
GET和POST两种基本请求方法的区别
GET和POST两种基本请求方法的区别
|
11月前
|
XML JSON 安全
get请求和post请求的区别以及常用请求方式
get请求和post请求的区别以及常用请求方式
|
JSON 数据格式
axios.post提交的三种请求方式
axios.post提交的三种请求方式
|
应用服务中间件 容器 数据安全/隐私保护
Servlet中request请求Get和Post方法以及乱码解决
前言: 传递的请求参数如何获取 GET方式: 参数放在URI后面 POST方式: 参数放在实体内容中 后台获取前台数据方法: 核心的API: request.getParameter("参数名"); 根据参数名获取参数值(注意,只能获取一个值的参数) request.
3030 0
|
JSON 数据格式 Java