3、基本的Get和Post访问(含代理请求)
Get方式访问
HttpClient hc = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("http://www.baidu.com?wd=HttpClient");
HttpResponse hr = hc.execute(request);
String body = EntityUtils.toString(hr.getEntity());
System.out.println(body);
Post方式访问
DefaultHttpClient hc = new DefaultHttpClient();
hc.setRedirectStrategy(new LaxRedirectStrategy()); //如果有跳转,就返回跳转后的内容 HttpPost hp = new HttpPost("http://www.baidu.com?wd=HttpClient"); List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "angelshelter"));
params.add(new BasicNameValuePair("keyword", "123")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
hp.setEntity(entity); HttpResponse hr = hc.execute(hp); String body = EntityUtils.toString(hr.getEntity()); System.out.println(body);
Proxy代理访问。
public class Main {
public static void main(String[] args) {
List<Thread> list = new ArrayList<Thread>();
for(int i=0;i<3;i++){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://wxgd.online.atianqi.com:8010/wxgdol/getobjdetail");
//HttpHost proxy = new HttpHost("localhost", 8888);
//RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//post.setConfig(config);
//HttpHost proxy = new HttpHost("localhost", 8888);
//client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
//post.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity entity = new StringEntity("{\"reqhead\":{\"servicecode\":\"OTTTV\",\"distributor\":\"SNM\",\"clienttype\":\"PC\",\"clientid\":\"JHSG7328f\",\"clientver\":\"1.0.1\",\"devicetype\":\"\",\"clientos\":\"andiod_4.3.1\"},\"reqbody\":{\"objectid\":\"news_1469974\",\"parentcode\":\"x_wx_yj_zy_bd_news\"}}", "utf-8");
post.setEntity(entity);
//使用代理
HttpResponse res = client.execute(post);
System.out.println("#" + EntityUtils.toString(res.getEntity()) + "#");
}catch(Exception e){
e.printStackTrace();
}
}
});
list.add(thread);
}
for(Thread t : list){
t.start();
}
}
}
3、基本的Get和Post访问(含代理请求)的更多相关文章
- 允许asp.net MVC报 错说明: 访问服务此请求所需的资源时出错。服务器可能未配置为访问所请求的 URL。错误消息 401.2。: 未经授权
运行mvc3程序报以下错误 详细报错如下: “/”应用程序中的服务器错误. 访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未 ...
- 【spring boot】spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面
问题描述: spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面 表现结果: 1>使用postman测试接口,表现为返回是40 ...
- requests接口自动化4-登录后才能访问的get请求,需共享cookie
登录后才能访问的get请求,需共享cookie fiddler里请求响应结果: 代码: import requests import json #form表单形式的post请求,用data传参,Con ...
- Vue部署到云服务器时,访问Nginx代理出现We're sorry but books doesn't work properly without JavaScript enabled. Please enable it to continue.
出现这个的原因,我这边的是Nginx的问题,因为没有匹配到静态文件的原因 第一个location是始终将访问的url请求定向到 index.html这个主页面 第二个location块是将index. ...
- google插件跨域含用户请求WebApi解决的方案
问题描述: google插件跨域请求WebApi相关解决方案 1.ajax解决含登录用户信息 $.ajax({ url: url, type: "POST", timeout: 6 ...
- 通过web代理进行跨域访问,http请求返回超时的问题定位
[现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.SocketTimeoutException: Read timed ...
- 跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp
为什么会设置同源策略 > 适用于浏览器的一种资源访问策略 > 同源策略(Same origin policy)是一种约定,它是浏览器最核 心也最 基本的安全功能,如果缺少了同源策略,则浏览 ...
- flutter 访问网页+http请求
一.目录 1.访问网页 2.http请求 -----------------------------这是分割线----------------------------- 1.访问网页 基于url_la ...
- mac nginx+php-fpm配置(安装过后nginx后访问php文件下载,访问php文件请求200显示空白页面)
访问php文件下载是因为没配置php-fpm 两个问题主要都是nginx.conf配置的问题: /usr/local/etc/nginx/nginx.conf server { listen 8 ...
随机推荐
- C编程常错项
linux系统下C编程一般报错;1,使用sqrt开平方函数未定义,是因为math.h[库,头文件未包含]2,隐式申明与内建函数"XXXX"不兼容;上述问题所在,一般是因为使用exi ...
- svn+apache+ssl快速部署
在svn+apache文章中已经成功搭建了web-svn,由于在http网络上数据都是以明文传输,公司的源码需要一定的保密机制,基于安全考虑现整合web-svn+ssl.构建安全的svn服务器, 1. ...
- Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件
转自:https://blog.csdn.net/zhen_6137/article/details/79383941
- WMI 连接远程计算机并进行局域网进程扫描
On Error Resume Next Dim myArray(231) myArray(0)="smss.exe"myArray(1)="csrss.exe" ...
- UI5-文档-4.22-Expression Binding
有时预定义的SAPUI5类型不够灵活,您希望在视图中执行简单的计算或格式化——这正是表达式真正有用的地方.我们使用它们根据数据模型中的当前数字格式化价格. Preview The price is n ...
- XmlHttpRequest对象 ajax核心之一
XMLHttpRequest 对象 XML XSLT XML 解析器 XMLHttpRequest 对象用于在后台与服务器交换数据. 什么是 XMLHttpRequest 对象? XMLHttpReq ...
- Jenkins服务器磁盘空间爆满问题解决
现象: 从根目录开始,使用du -h -x --max-depth=1 查看哪个目录占用过高,打算对于过高目录中的内容适当删减腾出一些空间 通过追踪查询,发现是jenkins的构建纪录未清理,而且最 ...
- Unified shader model
https://en.wikipedia.org/wiki/Unified_shader_model In the field of 3D computer graphics, the Unified ...
- Haskell语言学习笔记(49)ByteString Text
Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...
- Haskell语言学习笔记(59)Bitraversable
Bitraversable class (Bifunctor t, Bifoldable t) => Bitraversable t where bitraverse :: Applicativ ...