java代码编写http请求

独自空忆成欢 提交于 2019-12-17 23:02:01

java代码编写http请求

纯java代码模拟HTTP请求

// An highlighted block
package com.wk.cd.test.devcore.tk.service;

import com.wk.lang.AppException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.wk.cd.cdcommon.jnbk.service.HttpPublicService.closeStream;

public class testHttpRequest {

    public static void main(String[] args) {
        String urlParam = "http://116.236.14.218:8090/cdWeb/";
        StringBuffer result_buffer = new StringBuffer();
        HttpURLConnection con = null;
        BufferedReader in = null;
        OutputStreamWriter out = null;
        try {
            URL url = new URL(urlParam);
            con = (HttpURLConnection) url.openConnection();
            con.addRequestProperty("encoding", "UTF-8");
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Connection", "keep-alive");
            out = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            out.flush();
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String temp;
            while ((temp = in.readLine()) != null) {
                result_buffer.append(temp);
            }
        } catch (Exception e) {
            throw new AppException("http发送请求异常");
        } finally {
            closeStream(out, in, con);
        }
        System.out.println(result_buffer.toString());
    }

}

注意
此处没有添加更多的请求头信息,请求反爬虫网站(例如百度)会返回错误信息。
使用HttpURLConnection 的addRequestProperty方法填入更多的请头信息,就可以访问某些简单的反爬虫网站了

例如加入浏览器信息
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!