• HttpClient的使用(get、post请求)


    添加pom依赖

            <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.6</version>
            </dependency>
    

      

    get请求

    package com.qzcsbj;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * @公众号 : 全栈测试笔记
     * @博客 : www.cnblogs.com/uncleyong
     * @微信 : ren168632201
     * @描述 : <>
     */
    public class HttpGetRequest {
        public static void main(String[] args) {
            String url = "http://127.0.0.1:9999/download";
            String fname = "qzcsbj.txt";
            Map<String, String> params = new HashMap<String, String>();
            params.put("fname", fname);
            String res = getRequest(url, params);
            System.out.println("获取到的结果为:" + res);
    
        }
        public static String getRequest(String url, Map<String,String> params){
            String res = "";
            boolean flag = true;
            Set<String> keys = params.keySet();
            for (String key : keys) {
                if (flag){
                    url += "?" + key + "=" + params.get(key);
                    flag = false;
                }else {
                    url += "&" + key + "=" + params.get(key);
                }
            }
            HttpGet httpGet = new HttpGet(url);
            HttpClient httpClient = HttpClients.createDefault();
    
            try {
                HttpResponse response = httpClient.execute(httpGet);
                res = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;
        }
    }
    

    post请求

    package com.qzcsbj;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * @公众号 : 全栈测试笔记
     * @博客 : www.cnblogs.com/uncleyong
     * @微信 : ren168632201
     * @描述 : <>
     */
    public class HttpPostRequest {
        public static void main(String[] args) {
            String url = "http://127.0.0.1:9999/login";
            HashMap<String, String> params = new HashMap<String, String>();
            String username = "qzcsbj";
            String password = "123456";
            params.put("username", username);
            params.put("password", password);
            String res = postRequest(url, params);
            System.out.println("获取到的结果为:" + res);
        }
    
        public static String postRequest(String url, Map<String,String> params){
            String res = "";
            HttpPost httpPost = new HttpPost(url);
            ArrayList<BasicNameValuePair> basicNameValuePairs = new ArrayList<BasicNameValuePair>();
            Set<String> keys = params.keySet();
            for (String key : keys) {
                basicNameValuePairs.add(new BasicNameValuePair(key,params.get(key)));
            }
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(basicNameValuePairs,"utf-8"));
                HttpClient httpClient = HttpClients.createDefault();
                HttpResponse httpResponse = httpClient.execute(httpPost);
                res = EntityUtils.toString(httpResponse.getEntity());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;
        }
    }
    

      

    【bak】https://www.cnblogs.com/uncleyong/p/15859872.html

     原文:https://www.cnblogs.com/uncleyong/p/15867745.html

  • 相关阅读:
    多态
    接口和抽象类
    反射
    C++ 模板和 C# 泛型的区别
    基础类库中的泛型
    运行时中的泛型
    泛型代码中的 default 关键字
    泛型委托
    泛型方法
    泛型接口
  • 原文地址:https://www.cnblogs.com/uncleyong/p/15867745.html
Copyright © 2020-2023  润新知