java代码编写http请求

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值