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. PyQt5系列教程(九)QInputDialog的使用

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 输入框是界面开发中非常常见的控件,本文就来看看PyQt5中QInputDialog的使用 实例 ...

  2. Python环境搭建之OpenCV

    一.openCV介绍 Open Source Computer Vision Library.OpenCV于1999年由Intel建立,如今由Willow Garage提供支持.OpenCV是一个基于 ...

  3. C++官方文档-this

    #include <iostream> using namespace std; class Dummy { public: int x; Dummy() : x() { } ; Dumm ...

  4. Spring 配置 web.xml (防止spring 内存溢出)

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  5. leetcode961

    class Solution: def repeatedNTimes(self, A): doubleN = len(A) N = doubleN / 2 dic = {} for a in A: i ...

  6. OpenCV版本下载

    https://sourceforge.net/projects/opencvlibrary/files/opencv-win/

  7. C#怎么判断字符是不是汉字

    .用ASCII码判断 在 ASCII码表中,英文的范围是0-,而汉字则是大于127,根据这个范围可以判断,具体代码如下: string text = "我去"; bool res ...

  8. request error: Connection aborted.', error(113, 'No route to host')

    from: https://superuser.com/questions/720851/connection-refused-vs-no-route-to-host/720860 "Con ...

  9. How to Pronounce INTERNATIONAL

    How to Pronounce INTERNATIONAL Share Tweet Share Tagged With: Dropped T How do you pronounce this lo ...

  10. C++ 0x 使用 shared_ptr 自动释放, 防止内存泄漏

    最近在研究 cocos2d-x 3.0 ,它在创建类的对象时比如 Layer 时, 并不是直接使用 new , 而是使用一个宏方法  CREATE_FUNC(MyLayer);. 这个宏就是自动的创建 ...