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访问(含代理请求)的更多相关文章

  1. 允许asp.net MVC报 错说明: 访问服务此请求所需的资源时出错。服务器可能未配置为访问所请求的 URL。错误消息 401.2。: 未经授权

    运行mvc3程序报以下错误 详细报错如下: “/”应用程序中的服务器错误. 访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未 ...

  2. 【spring boot】spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面

    问题描述: spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面 表现结果: 1>使用postman测试接口,表现为返回是40 ...

  3. requests接口自动化4-登录后才能访问的get请求,需共享cookie

    登录后才能访问的get请求,需共享cookie fiddler里请求响应结果: 代码: import requests import json #form表单形式的post请求,用data传参,Con ...

  4. 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. ...

  5. google插件跨域含用户请求WebApi解决的方案

    问题描述: google插件跨域请求WebApi相关解决方案 1.ajax解决含登录用户信息 $.ajax({ url: url, type: "POST", timeout: 6 ...

  6. 通过web代理进行跨域访问,http请求返回超时的问题定位

    [现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.SocketTimeoutException: Read timed ...

  7. 跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp

    为什么会设置同源策略 > 适用于浏览器的一种资源访问策略 > 同源策略(Same origin policy)是一种约定,它是浏览器最核 心也最 基本的安全功能,如果缺少了同源策略,则浏览 ...

  8. flutter 访问网页+http请求

    一.目录 1.访问网页 2.http请求 -----------------------------这是分割线----------------------------- 1.访问网页 基于url_la ...

  9. mac nginx+php-fpm配置(安装过后nginx后访问php文件下载,访问php文件请求200显示空白页面)

    访问php文件下载是因为没配置php-fpm 两个问题主要都是nginx.conf配置的问题: /usr/local/etc/nginx/nginx.conf server {    listen 8 ...

随机推荐

  1. django-paginator

    py code... from django.core.paginator import Paginator class NewsListView(View): def get(self, reque ...

  2. 外星人完事了,开始python的matplotlib玩转

    外星人完事了,开始python的matplotlib玩转 看书上的例子,在win下安装比较麻烦 今天用pip尝试了一下 pip install matplotlib 然后等待即可 安装完毕后 在pyt ...

  3. php中点击链接直接下载图片

    最近需要一个功能,是点击链接,直接把图片下载下来,一般情况下,图片是在新页直接打开的,不会自动提示下载,在网上找来找,用这个挺好使,代码如下: $filename = basename($downfi ...

  4. 2. java获取下周日-下周六的时间

    String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...

  5. viewer 照片查看器

    viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...

  6. fb bin_debug下的swf不见了

    fb清理了所选的项目,如果代码有错误,会自动删除bin_debug目录下的swf.这种情况,构建项目是无法自动生成swf的,只有将代码报错的地方修改正错了.选构建项目才会在bin_debug目录下生成 ...

  7. sts,eclipse里面配置tomcat

    第一步:点击window-->preferences-->server-->Runtime Environments点击add. 第二步:选择本机上面有的tomcat版本点击next ...

  8. WP 8.1 status bar

    A status bar is the bar showing signal, battery and time on the top of the phone's screen. In WP8.1 ...

  9. PCM 编码

    PCM编码,即无损编码(抽样->量化->编码) 这里的 无损 是个广义概念,任何数字编码都有损,只不过PCM的“损”最小:通常所说的无损编码都是指PCM编码 wav音频参数: 最重要的三个 ...

  10. Nginx打开目录浏览功能(autoindex)以及常见问题解决方案

    Nginx默认是不允许列出整个目录的.如需此功能,打开nginx.conf文件,在location server 或 http段中加入autoindex on;另外两个参数最好也加上去: autoin ...