zoukankan      html  css  js  c++  java
  • Java HTTP请求

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.zip.GZIPInputStream;
    public class testHTTP {
        public static void main(String[] args) throws IOException
        {
            try 
            {
                URL url = new URL("http://zhidao.baidu.com/ ");
                URLConnection conn = url.openConnection();
                HttpURLConnection httpURLConnection = (HttpURLConnection)conn;
                httpURLConnection.setRequestProperty("Host", "zhidao.baidu.com");
                httpURLConnection.setRequestProperty("connection", "Keep-Alive");
                httpURLConnection.setRequestProperty("Cache-Control", "max-age=0");
                httpURLConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0");
                httpURLConnection.setRequestProperty("Referer", "https://www.baidu.com/link?url=BHPFr4z5_jfNStMOzkornuMJSmwj_2dOif9q8Ivqw9Rw8xPahOvXdFJKgKpSSpiYGhynort7ziken5eO1Q207a&wd=&eqid=a08f92f0000022f80000000355eece3b");
                //关键是下面这句话   这句话的意思是浏览器支持的解压缩方式  如果发送这个报头,那么服务器返回的就可能是以某种方式压缩后的信息
                //httpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
                httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
                
                httpURLConnection.setRequestProperty("Cookie", "BAIDUID=93F317FBB6B90290B30A5C8521ADF385:FG=1; PSTM=1441426636; BIDUPSID=F3BB3C015AB71995F023187B9424234B; bdshare_firstime=1441714095462; BDRCVFR[Zln_T-KFICf]=pdg3iqubLhtTvqMULR8mvqV; H_PS_PSSID=1431_13477_12868_17104_16935_17004_15648_11513_13932_16969_17051; IK_93F317FBB6B90290B30A5C8521ADF385=4; IK_CID_74=4; Hm_lvt_6859ce5aaf00fb00387e6434e4fcc925=1441714095,1441714137,1441714225,1441715740; Hm_lpvt_6859ce5aaf00fb00387e6434e4fcc925=1441717326");
                
                conn.setDoOutput(true);
                conn.setDoInput(true);
                
                String result = "";
                BufferedReader in = null;
                PrintWriter out = null;
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                //out.print(param);
                // flush输出流的缓冲
                out.flush();
                //GBK与UTF-8 是传输过程中常用的两种编码方式
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"GBK"));
                String line;
                while ((line = in.readLine()) != null) 
                {
                    result += line;
                }
                
                System.out.println(result);
                
            } 
            catch (MalformedURLException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
        
    }

    关键点有二:第一发送的时候不要发送 httpURLConnection.setRequestProperty("Accept-Encoding", "gzip");  这个报头,否则接受到的则是以gzip方式压缩后的数据

          第二时用的inputstream  注意编码方式

  • 相关阅读:
    Oracle建立表空间和用户
    fscanf()函数具体解释
    三层架构(我的理解及具体分析)
    ListView嵌套ListView优化
    Android xml 解析
    玩转Web之servlet(三)---一张图看懂B/S架构
    jquery.scrollTo-min.js
    C#中MessageBox使用方法大全(附效果图)
    hdu 1882 Strange Billboard(位运算+枚举)
    MySQL 通配符学习小结
  • 原文地址:https://www.cnblogs.com/tengpan-cn/p/4802601.html
Copyright © 2011-2022 走看看