HttpClient(JAVA)使用笔记
HTTPCLIENT
此工具是由apache基金会支持开发的一套 开源 http client 组件, 目前属于 http components的一部分,
官网:http://hc.apache.org/index.html
http components 健壮地实现了基础HTTP部分, 并可以被扩展, 可以被 客户端和服务器端 HTTP 应用程序使用, 例如 浏览器 爬虫 代理 和 web service, 以及利用和扩展http协议用于分布式通信的系统。
http components包括:
-- http core 实现了http基础协议,包括客户端和服务器端,可以定制阻塞 和 非阻塞模式, 非阻塞模式对网络吞吐量有利,非阻塞模式对数据密集情况有利。
-- http client 基于http core实现遵守http1.1的协议,还包括客户端认证、状态管理和连接管理。
-- http asyncclient, 基于上面两个,实现非阻塞模式的客户端, 有利吞吐量。
本文以 http client作为研究对象,
http client总体文档 http://hc.apache.org/httpcomponents-client-4.3.x/index.html
API http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/index.html
有很多例子可供参考 http://hc.apache.org/httpcomponents-client-ga/examples.html
实验-目的
通过http client工具, 登陆路由器,重启系统(网速很慢的时候, 重启下路由器,希望能快点),要求能够检测启动中和启动完毕。
实验-步骤
1、 下载 http client(httpcomponents-client-4.3.3-bin.tar.gz)
http://mirrors.cnnic.cn/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.3.3-bin.zip
2、eclipse新建一个工程(httpclient),
创建一个包(org.apache.http.examples.client),
创建一个类(ClientAuthentication)。
3、导入 http client 压缩包, 并将压缩包中的lib文件下的所有jar文件,添加为user libraries。
4、参考 examples.html 上的 basic 登陆例子,实现登陆, 并发起重启报文, 然后再启动子进程发送ping诊断检测设备启动过程。
代码
package org.apache.http.examples.client; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /** * A simple example that uses HttpClient to execute an HTTP request against * a target site that requires user authentication. */ public class ClientAuthentication { public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("192.168.1.1", 80), new UsernamePasswordCredentials("username", "password")); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); try { //HttpGet httpget = new HttpGet("http://192.168.1.1/"); HttpGet httpget = new HttpGet("http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%CF%B5%CD%B3"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } String cmd = "ping 192.168.1.1 -t"; //返回与当前 Java 应用程序相关的运行时对象 Runtime run = Runtime.getRuntime(); try { // 启动另一个进程来执行命令 Process p = run.exec(cmd); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); String lineStr; while ((lineStr = inBr.readLine()) != null) //获得命令执行后在控制台的输出信息 System.out.println(lineStr);// 打印输出信息 //检查命令是否执行失败。 if (p.waitFor() != 0) { if (p.exitValue() == 1)//p.exitValue()==0表示正常结束,1:非正常结束 System.err.println("命令执行失败!"); } inBr.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } }
打印输出
Executing request GET http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%CF%B5%CD%B3 HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=82ms TTL=64
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=2ms TTL=64
Reply from 192.168.1.1: bytes=32 time=6ms TTL=64