zoukankan      html  css  js  c++  java
  • java+httpclient—— 一个简单的post请求

    package jkcs;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    
    public class jiekoumoni 
    {
    
        public static void main(String[] args) throws ClientProtocolException, IOException, Exception 
        {
        
            
            CloseableHttpClient client = HttpClients.createDefault();    //创建一个http客户端
            
            HttpPost httpPost = new HttpPost("http://www.oschina.net/");// 通过httpPost方式来实现我们的get请求
            
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//伪装成浏览器请求
            
            CloseableHttpResponse Response = client.execute(httpPost);  // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
            
            
            //-----------------------------------------------------------------------
    
            
            System.out.println(Response.getStatusLine());  //HTTP/1.1 200 OK
            
            System.out.println(Response.getProtocolVersion());  //HTTP/1.1
            
            System.out.println(Response.getStatusLine().getStatusCode());  //200
            
            
            //-----------------------------------------------------------------------
            
            
            
            HttpEntity httpEntity = Response.getEntity();         //获取某个固定的响应头
            
            System.out.println(httpEntity.getContentType()); //Content-Type: text/html
            
            
    
            
            
            //-----------------------------------------------------------------------
            
            
            System.out.println(Response.getFirstHeader("Content-Type"));//Content-Type: text/html
            System.out.println(Response.getFirstHeader("Date"));   //Date: Sun, 03 May 2020 06:58:16 GMT
            
            //-----------------------------------------------------------------------
            
            Header[] headers = Response.getAllHeaders();    //获取所有响应头
            for (Header header : headers) 
            {
               System.out.println("Key : " + header.getName()+",         ," +" Value : " + header.getValue());
            }
            
            
            
            //-----------------------------------------------------------------------
            
            
            
            Response.close();  // 关闭
            
            
            
    
        }
    
    }

    执行结果:

    HTTP/1.1 301 Moved Permanently
    HTTP/1.1
    301


    Content-Type: text/html
    Content-Type: text/html
    Date: Sun, 03 May 2020 07:47:35 GMT


    Key : Date, , Value : Sun, 03 May 2020 07:47:35 GMT
    Key : Content-Type, , Value : text/html
    Key : Content-Length, , Value : 239
    Key : Connection, , Value : keep-alive
    Key : Server, , Value : Tengine
    Key : Location, , Value : https://www.oschina.net/

  • 相关阅读:
    TFS对签入文件忽略设置,解决pdb弹出警告
    利用MSSQL对不经常使用的表进行依赖缓存
    IIS 使用多个https和通配证书解决方案
    传递参数安全验证代码示例
    利用GetType反射方法再调用方法进行传递参数实现调用
    ms10_002 IE浏览器漏洞
    ms17-010漏洞复现
    ms08-067漏洞复现
    tomcat使用log4j管理日志
    Popen No such file or directory 错误
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12822186.html
Copyright © 2011-2022 走看看