zoukankan      html  css  js  c++  java
  • httpclient向浏览器发送get和post请求

    get请求代码实现

    public static void main(String[] args) {

           CloseableHttpClient httpClient = null;  //请求对象

           CloseableHttpResponse response = null;  //返回对象

           HttpEntity entity = null;  //返回主体

           String responseContent = null; //将返回的主题转换成字符串

           String url = "http://127.0.0.1:5000";//请求的URL

           try {

      httpClient = HttpClients.createDefault();

    HttpGet httpGet = new HttpGet(url);

    response = httpClient.execute(httpGet);

    entity = response.getEntity();

    responseContent = EntityUtils.toString(entity, "utf-8");

    System.out.println(httpGet.getURI());

    System.out.println(responseContent);

    httpClient.close();

             } catch (IOException e) {

    e.printStackTrace();

            }

    }

    post请求代码实现

    public static void main(String[] args) {

            CloseableHttpClient httpClient = null;

            CloseableHttpResponse response = null;

            HttpEntity entity = null;  

            String responseContent = null;

            String url = "http://127.0.0.1:5000/login";

           try {

    httpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("username", "bokeyuan"));

    nameValuePairs.add(new BasicNameValuePair("password", "123456"));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

    response = httpClient.execute(httpPost);

    entity = response.getEntity();

    responseContent = EntityUtils.toString(entity, "utf-8");

    System.out.println(responseContent);

    httpClient.close();

                   } catch (IOException e) {

    e.printStackTrace();

    }

    }

  • 相关阅读:
    ASP.NET的最新安全漏洞Important: ASP.NET Security Vulnerability
    Sql常用日期格式
    倒计时 服务器时间 .NET js javascript
    “备份集中的数据库备份与现有的数据库不同”解决方法
    2010年最佳jQuery插件
    jQuery1.4与json格式兼容问题
    .NET结束外部进程 C#结束外部进程
    十步优化SQL Server中的数据访问
    SQL游标的使用与语法
    SQL2005、SQL2008如何压缩日志文件(log) 如何清除日志
  • 原文地址:https://www.cnblogs.com/Zcxxf/p/6612071.html
Copyright © 2011-2022 走看看