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

Java http 请求三种形式

2018-03-20 12:01 134 查看
JavaHttp接口交互的两种方式
    JavaHttp接口开发针对表单提交跟Json提交的两种方式
    表单提交(post、get)
        get表单参数提交

/**
     * @param type
     * @param url
     * @param map
     * @return
     * GY
     * 2018年1月2日
     * url:请求地址
     * map:请求参数
     * Get有参请求
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static String
HttpSendGet(String url, Map<String,String> map)
            throws ClientProtocolException,
IOException{
        String resultStr = null;
        if(map==null){
            return resultStr;
        }
        CloseableHttpClient httpclient =
HttpClients.createDefault();
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (Map.Entry<String,
String> entry : map.entrySet())
{
            //给参数赋值
            //gson.toJson(entry.getValue())
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        String paramStr =
EntityUtils.toString(new UrlEncodedFormEntity(formparams,
Consts.UTF_8));
        HttpGet httpget = new HttpGet(url+"?"+paramStr);
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity responseEntity = response.getEntity();
        if(responseEntity != null){
            resultStr =
EntityUtils.toString(responseEntity);
        }
        if(httpclient!=null){
            httpclient.close();
        }
        if(response!=null){
            response.close();
        }
        return resultStr;
    }

         post表单提交

/**
     * @param url
     * @param map
     * @return
     * GY
     * 2018年1月2日
     * url:请求地址
     * map:请求参数
     * Post有参请求
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static String
HttpSendPost(String url, Map<String,String> map)
            throws ClientProtocolException,
IOException{
        if(map==null){
            return null;
        }
        String resultStr = null;
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient =
HttpClients.createDefault();
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (Map.Entry<String,
String> entry : map.entrySet())
{
            //给参数赋值
            //gson.toJson(entry.getValue())
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
        //System.out.println(EntityUtils.toString(entity));
        //2.生成一个post请求
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        //3.执行post请求并返回结果
        CloseableHttpResponse response = httpclient.execute(httppost);
        //4.处理结果,这里将结果返回为字符串
        HttpEntity responseEntity = response.getEntity();
        if(responseEntity != null){
            resultStr =
EntityUtils.toString(responseEntity);
        }
        if(httpclient!=null){
            httpclient.close();
        }
        if(response!=null){
            response.close();
        }
        return resultStr;
    }

         get无参提交

/**
     * 发送HttpGet请求
     * @param url
     * @return
     * @throws IOException
     * @throws ClientProtocolException
     * GY
     * 2018年1月2日
     * Get无参请求
     */
    public static String
sendGet(String url) throws ClientProtocolException,
IOException {
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient =
HttpClients.createDefault();
        //2.生成一个get请求
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        //3.执行get请求并返回结果
        response = httpclient.execute(httpget);
        String result = null;
        //4.处理结果,这里将结果返回为字符串
        HttpEntity entity = response.getEntity();
        if (entity != null)
{
            result =
EntityUtils.toString(entity);
        }
        if(httpclient!=null){
            httpclient.close();
        }
        if(response!=null){
            response.close();
        }
        return result;
    }

    Json数据类型提交

/**
     * @param url
     * @param json
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * GY
     * 2018年1月4日
     * 发送json 数据 http client 
     */
    public static String
HttpSendJsonPost(String url, String json)
            throws ClientProtocolException,
IOException{
        if(StringUtils.isEmpty(json)){
            return null;
        }
        String resultStr = null;
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient =
HttpClients.createDefault();
        StringEntity  postingString = new StringEntity(json,
Consts.UTF_8);//
json传递
        postingString.setContentEncoding("UTF-8");
        postingString.setContentType("application/json");  
        System.out.println(EntityUtils.toString(postingString));
        //2.生成一个post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(postingString);
        //3.执行post请求并返回结果
        CloseableHttpResponse response = httpclient.execute(httpPost);
        //4.处理结果,这里将结果返回为字符串
        HttpEntity responseEntity = response.getEntity();
        if(responseEntity != null){
            resultStr =
EntityUtils.toString(responseEntity);
        }
        if(httpclient!=null){
            httpclient.close();
        }
        if(response!=null){
            response.close();
        }
        return resultStr;
    }

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