使用httpClient访问指定网址

对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用。

注意:对于URL必须使用 http://开始,否则会有如下报错信息:

Caused by: org.apache.http.ProtocolException: Target host is not specified

at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:69)

at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)

... 5 more

demo代码:

public class ClientWithResponseHandler {

public static String url = "http://www.baidu.com";

public final static void main(String[] args) throws Exception {

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

HttpGet httpget = new HttpGet(url);

System.out.println("Executing request " + httpget.getRequestLine());

// Create a custom response handler

ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

public String handleResponse(

final HttpResponse response) throws ClientProtocolException, IOException {

int status = response.getStatusLine().getStatusCode();

if (status >= 200 && status < 300) {

HttpEntity entity = response.getEntity();

return entity != null ? EntityUtils.toString(entity) : null;

} else {

throw new ClientProtocolException("Unexpected response status: " + status);

}

}

};

String responseBody = httpclient.execute(httpget, responseHandler);

System.out.println("----------------------------------------");

System.out.println(responseBody);

} finally {

httpclient.close();

}

}

}

时间: 2024-08-03 19:02:33

使用httpClient访问指定网址的相关文章

js实现域名判断后跳转到指定网址

js实现域名判断后跳转到指定网址,也适用于同一虚拟空间放多个网站: <script>       try           {               if(self.location == "http://apple.ya37.com/")               {                   top.location.href = "http://ya37.com/apple/";               }         

java网络访问指定出口ip

java网络访问指定出口ip Table of Contents 1. socket 2. apache httpclient 1 socket 可以在Socket构造函数中指定使用的本地ip,如: Socket socket = new Socket("127.0.0.1", 12345, InetAddress.getByAddress(new byte[] { new Integer(10).byteValue(), new Integer(211).byteValue(), n

dos下通过命令访问url网址

psexec.exe工具: https://blog.csdn.net/feier7501/article/details/8841756 https://www.cnblogs.com/boltkiller/articles/4791307.html https://www.cnblogs.com/sela/p/5506531.html https://blog.csdn.net/feier7501/article/details/8847172 问题背景: 自动化部署之前一直都是linux文

vue-devtools安装以后,勾选了“允许访问文件网址”之后还是无法使用

勾选了"允许访问文件网址",还是无法使用: Vue.js is detected on this page. Devtools inspection is not available because it's in production mode or explicitly disabled by the author 在vue-devtools的github中有这样一句说明: If the page uses a production/minified build of Vue.js

使用SSH代理实现访问国外网址

1. 在SSH服务器端创建用户: useradd -M -s /bin/false username passwd username 2. 安装myentunnel软件 (Linux用户可使用:ssh -CfNg -D 127.0.0.1:7070 用户名@服务器地址) 3. 打开Firefox 4. 修改Firefox代理设置: 5. 以管理员身份运行myentunnel或者参考下图: 6. 填入在SSH服务器端建立的用户名和密码 (useradd -m username) 7. 点击conn

sqlserver 创建用户只能访问指定视图

use crm --当前数据库创建角色 exec sp_addrole 'rapp' --分配视图权限 GRANT SELECT  ON veiw TO [角色] --指定视图列表 GRANT SELECT ON  v_city TO rapp GRANT SELECT ON  v_lp TO rapp GRANT SELECT ON  v_qy TO rapp go --添加只允许访问指定视图的用户: exec sp_addlogin '登录名','密码','默认数据库名' exec sp_a

访问指定路径下的目录以及文件

#include "stdafx.h" //vs2010下运行通过 #undef UNICODE #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <iostream> using namespace std; void browseFile(char* path) { char pattern[FILENAME_MAX + 1]; sprintf(p

vsftpd添加用户并限制目录(指定用户访问指定目录)

开发那边更新速度如果比较快,每天会有无数的文件或者目录交给运维更新上线,这样比较麻烦,干脆给他们搭FTP让他们自己上传修改,但有一问题,就是权限问题,公司五个站点有五个目录,每个人负责不同的站点,如何让各自都不干扰别人,或者说不到别人的目录搞“破坏”!下面解决这个问题! 一.检查Linux中vsftpd的安装情况. 就不啰嗦了 二.将系统中的SELinux关闭,然后重新启动电脑(不关闭的话修改好标签也可以) 关闭SELinux的方法: 修改/etc/selinux/config文件中的SELIN

允许访问文件网址 chrome插件markdown preview plus

装完这个插件后进把本地文件的地址输到地址栏,结果就是不能解释md文件.始终没注意到在扩展程序管理里面,这个插件的还有个选项“允许访问文件网址”,尼玛,这是怎么翻译的呀.这个选项是说要不要解释本地的文件.....看了一下 markdown preview的说明:1. Install extension from webstore (creates no new UI) 2. Check "Allow access to file URLs" in chrome://extensions