两种方式:
1、Standard HttpURLConnection.
2、Apache HttpClient library.
第一种方式:Java HttpURLConnection example
从GET和POST这两方面实践。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "http://www.google.com/search?q=test";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
第二种方式:Apache HttpClient
从GET和POST这两方面实践。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class HttpClientExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpClientExample http = new HttpClientExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "http://www.google.com/search?q=developer";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://selfsolve.apple.com/wcResults.do";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("User-Agent", USER_AGENT);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM"));
urlParameters.add(new BasicNameValuePair("cn", ""));
urlParameters.add(new BasicNameValuePair("locale", ""));
urlParameters.add(new BasicNameValuePair("caller", ""));
urlParameters.add(new BasicNameValuePair("num", "12345"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
}
分享到:
相关推荐
研究API:查看Bilibili是否提供了公开的API接口,如果没有,你可能需要分析网页结构,使用如requests和BeautifulSoup等库来模拟HTTP请求并解析页面内容。 编写爬虫:使用Python编写爬虫代码,注意遵守Bilibili的...
在房产技术部的实践中,Mock 服务平台使用 mitmproxy 作为代理服务,对所有经过此代理服务器的请求/响应进行过滤,匹配到是需要 Mock 的请求就将预期的 Mock 数据直接返回。也可以在真实响应到达代理服务时,对响应...
遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...
遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...
使用状态机解析HTTP请求报文,支持解析GET和POST请求 访问服务器数据库实现web端用户注册、登录功能,可以请求服务器图片和视频文件 实现同步/异步日志系统,记录服务器运行状态 经Webbench压力测试可以实现上万的...
遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施...
Python Web Scraping项目是一个综合性教程和实践项目,旨在帮助用户掌握使用Python进行网页数据抓取的技能。该项目涵盖了从基础到高级的Web Scraping技术,结合实际案例,详细讲解了如何使用各种工具和库进行网页...
在本文中,我们将编写一个基于JWT进行身份验证的ASP.NET Web API demo,以模拟前后端分离的开发方式。该demo包含一个静态页站点(在IIS中的路径为http://localhost:8057)以及一个Web API站点...
例如,Python标准库中的`urllib`和第三方库`requests`被广泛用于模拟HTTP请求,发送GET和POST数据,并处理响应内容;而更高级的爬虫框架如Scrapy和pyspider则提供了完整的爬虫生命周期管理、数据解析(XPath、CSS...
3. 爬虫原理:爬虫通过发送HTTP请求获取网页内容,然后对网页进行解析,提取所需的数据。解析网页的方法主要包括正则表达式、XPath和CSS选择器等。 三、爬虫开发环境搭建 1. 安装Python:Python是爬虫开发的主要语言...
9.2.9 根据TCP选项调整数据长度 9.3 小结 第10章 T/TCP实现:TCP函数 10.1 概述 10.2 tcp_newtcpcb函数 10.3 tcp_rtlookup函数 10.4 tcp_gettaocache函数 10.5 重传超时间隔的计算 10.6 tcp_close函数 10.7 tcp_...
中科大工程实践大作业代号 Serial,通过 Java 实现 Windows 程序串口通信,并将数据通过 HTTP 请求实时发送到云端服务器。 Prerequisite Java VS C/C++ 一般来说,串口通信通常由 C/C++ 程序实现会更加方便,但由于...
第二步(掌握工具):深入介绍LoadRunner 11工具三大部分(Virtual User Generator、Controller、Analysis)如何实现用户行为的模拟、性能指标的监控、负载的生成及后期的数据分析;第三步(项目实施):理论联系...
第二步(掌握工具):深入介绍LoadRunner 11工具三大部分(Virtual User Generator、Controller、Analysis)如何实现用户行为的模拟、性能指标的监控、负载的生成及后期的数据分析;第三步(项目实施):理论联系...
第二步(掌握工具):深入介绍LoadRunner 11工具三大部分(Virtual User Generator、Controller、Analysis)如何实现用户行为的模拟、性能指标的监控、负载的生成及后期的数据分析;第三步(项目实施):理论联系...
现在我们开始填充一 个测试计划的内容,这个测试计划 " "向一个jsp文件和一个servlet发出请求,我们需要JMeter模拟五个请求者(也就是五个线" "程),每个请求者连续 请求两次。 " ----------------------- JMeter...
这涉及到后端服务器的交互,因此项目中可能包含网络请求的代码,如使用Retrofit或Volley等库进行HTTP通信。数据库使用:为了存储用户信息、商品数据等,项目可能会集成SQLite数据库或使用Room持久性库。这样可以在...
2.5.3 HTTP请求 2.5.4 HTTP应答 2.5.5 HTTP捕获 2.5.6 HTTP回放 2.6 安装 2.6.1 在Windows下安装LoadRunner 2.6.2 安装Load Generator 2.6.3 附加组件 2.6.4 LoadRunner License 2.7 LoadRunner性能测试...
2.5.3 HTTP请求 2.5.4 HTTP应答 2.5.5 HTTP捕获 2.5.6 HTTP回放 2.6 安装 2.6.1 在Windows下安装LoadRunner 2.6.2 安装Load Generator 2.6.3 附加组件 2.6.4 LoadRunner License 2.7 LoadRunner性能测试...
2.5.3 HTTP请求 2.5.4 HTTP应答 2.5.5 HTTP捕获 2.5.6 HTTP回放 2.6 安装 2.6.1 在Windows下安装LoadRunner 2.6.2 安装Load Generator 2.6.3 附加组件 2.6.4 LoadRunner License 2.7 LoadRunner性能测试...