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