1)使用HttpClient发送GET请求

public class MainActivity extends Activity implements OnClickListener {

    private Button btnGet;
private WebView wView;
public static final int SHOW_DATA = 0X123;
private String detail = ""; private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == SHOW_DATA)
{
wView.loadDataWithBaseURL("",detail, "text/html","UTF-8","");
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setView();
} private void initView() {
btnGet = (Button) findViewById(R.id.btnGet);
wView = (WebView) findViewById(R.id.wView);
} private void setView() {
btnGet.setOnClickListener(this);
wView.getSettings().setDomStorageEnabled(true);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnGet) {
GetByHttpClient();
}
}
private void GetByHttpClient() {
new Thread()
{
public void run()
{
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.w3cschool.cc/python/python-tutorial.html");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
detail = EntityUtils.toString(entity, "utf-8");
handler.sendEmptyMessage(SHOW_DATA);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
} }

2)使用HttpClient发送POST请求

private void PostByHttpClient(final String url)
{
new Thread()
{
public void run()
{
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "猪大哥"));
params.add(new BasicNameValuePair("pawd", "123"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity2 = httpResponse.getEntity();
detail = EntityUtils.toString(entity2, "utf-8");
handler.sendEmptyMessage(SHOW_DATA);
}
}catch(Exception e){e.printStackTrace();}
};
}.start();
}

HttpClient抓数据示例(教务系统数据抓取)

HttpClient可以通过下述代码获取与设置Cookie: HttpResponse loginResponse = new DefaultHttpClient().execute(getLogin); 获得Cookie:cookie = loginResponse.getFirstHeader("Set-Cookie").getValue(); 请求时带上Cookie:httpPost.setHeader("Cookie", cookie);

/获得链接,模拟登录的实现:
public int getConnect(String user, String key) throws Exception {
// 先发送get请求 获取cookie值和__ViewState值
HttpGet getLogin = new HttpGet(true_url);
// 第一步:主要的HTML:
String loginhtml = "";
HttpResponse loginResponse = new DefaultHttpClient().execute(getLogin);
if (loginResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = loginResponse.getEntity();
loginhtml = EntityUtils.toString(entity);
// 获取响应的cookie值
cookie = loginResponse.getFirstHeader("Set-Cookie").getValue();
System.out.println("cookie= " + cookie);
} // 第二步:模拟登录
// 发送Post请求,禁止重定向
HttpPost httpPost = new HttpPost(true_url);
httpPost.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); // 设置Post提交的头信息的参数
httpPost.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
httpPost.setHeader("Referer", true_url);
httpPost.setHeader("Cookie", cookie); // 设置请求数据
List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("__VIEWSTATE",
getViewState(loginhtml)));// __VIEWSTATE参数,如果变化可以动态抓取获取
params.add(new BasicNameValuePair("Button1", ""));
params.add(new BasicNameValuePair("hidPdrs", ""));
params.add(new BasicNameValuePair("hidsc", ""));
params.add(new BasicNameValuePair("lbLanguage", ""));
params.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));
params.add(new BasicNameValuePair("txtUserName", user));
params.add(new BasicNameValuePair("TextBox2", key));
params.add(new BasicNameValuePair("txtSecretCode", "")); // ( ╯□╰ )逗比正方,竟然不需要验证码 // 设置编码方式,响应请求,获取响应状态码:
httpPost.setEntity(new UrlEncodedFormEntity(params, "gb2312"));
HttpResponse response = new DefaultHttpClient().execute(httpPost);
int Status = response.getStatusLine().getStatusCode();
if(Status == 200)return Status;
System.out.println("Status= " + Status); // 重定向状态码为302
if (Status == 302 || Status == 301) {
// 获取头部信息中Location的值
location = response.getFirstHeader("Location").getValue();
System.out.println(location);
// 第三步:获取管理信息的主页面
// Get请求
HttpGet httpGet = new HttpGet(ip_url + location);// 带上location地址访问
httpGet.setHeader("Referer", true_url);
httpGet.setHeader("Cookie", cookie); // 主页的html
mainhtml = "";
HttpResponse httpResponseget = new DefaultHttpClient()
.execute(httpGet);
if (httpResponseget.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponseget.getEntity();
mainhtml = EntityUtils.toString(entity);
} }
return Status;
}

使用HttpPut发送Put请求

public static int PutActCode(String actCode, String licPlate, Context mContext) {
int resp = 0;
String cookie = (String) SPUtils.get(mContext, "session", "");
HttpPut httpPut = new HttpPut(PUTACKCODE_URL);
httpPut.setHeader("Cookie", cookie);
try { List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("activation_code", actCode));
params.add(new BasicNameValuePair("license_plate", licPlate));
httpPut.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse course_response = new DefaultHttpClient().execute(httpPut);
if (course_response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity2 = course_response.getEntity();
JSONObject jObject = new JSONObject(EntityUtils.toString(entity2));
resp = Integer.parseInt(jObject.getString("status_code"));
return resp;
}
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}

HttpClient使用示例的更多相关文章

  1. HttpClient SSL示例(转)

    原文地址: http://www.cnblogs.com/jerry19890622/p/4291053.html package com.jerry.httpclient; import java. ...

  2. [转][C#]HttpClient 代码示例

    转自:https://www.cnblogs.com/amosli/p/3918538.html 也参考了:https://www.cnblogs.com/ShadowFiend007/p/80668 ...

  3. angular5 httpclient的示例实战

    摘要: 从angular 4.3.0 以后的版本开始使用httpclient,替换了之前的http,引用的包路径已经变为了angular/common/http了 一个基础的 httpclient 样 ...

  4. Table of Contents - HttpClient

    HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...

  5. yii2 httpClient的用法

    yii2 httpClient的用法示例: <?php /* * @Purpose : yii2 httpClient 请求示例 * @Author : Chrdai * @Time : 201 ...

  6. java访问Https服务的客户端示例

    关于证书 1.每个人都可以使用一些证书生成工具为自己的https站点生成证书(比如JDK的keytool),大家称它为“自签名证书”,但是自己生成的证书是不被浏览器承认的,所以浏览器会报安全提示,要求 ...

  7. 关于.NET HttpClient方式获取微信小程序码(二维码)

    随着微信小程序的火热应用,市面上有关小程序开发的需求也多了起来.近来分析了一项生成有关生成微信小程序码的需求——要求扫码跳转到小程序指定页面(带参数):看了下小程序官方文档文档,结合网上的例子,未看到 ...

  8. HttpClient到底该不该using?

    HttpClient实例是否应该释放掉? 从源代码中可以的看到httpClient类最上层实现了IDisposable接口,看到该接口我们下意识就是要用using(自动释放)代码块包含起.或者自己手动 ...

  9. HttpClient类详解

    文章链接:https://blog.csdn.net/justry_deng/article/details/81042379 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了 ...

随机推荐

  1. 状态保持: cookier及session简介

    状态保持 因为 http 是一种无状态协议,浏览器请求服务器是无状态的. 无状态:指一次用户请求时,浏览器.服务器无法知道之前这个用户做过什么,每次请求都是一次新的请求. 无状态原因:浏览器与服务器是 ...

  2. 为什么叫金拱门- golden arch

    不要再纠结为什么叫这么难理解的名字了.因为从golden arch直译过来的撒.金色的拱门.就叫金拱门咯. 关于M的商标的历史来源如下: "McDonald's logo" red ...

  3. Android 屏幕密度适配

    Android Icon Size and Location for Apps   分辨率 DPI Density scale 1dp对应像素 1dp对应物理尺寸 Location Icon Size ...

  4. linux和windows双系统设置默认启动系统

    1.以root身份登录linux系统: 2.启动终端,输入命令:vi /boot/grub/grub.conf,文件内容如下: #boot=/dev/sda default=0 (默认启动哪个系统的设 ...

  5. 添加机构organizations模块

    startapp organizations models内容: from django.db import models from datetime import datetime # Create ...

  6. 《Unity 3D游戏客户端基础框架》系统设计

    引言 最近到看一个 <贪吃蛇大战开发实例>,其中 贪吃蛇大作战游戏开发实战(3):系统构架设计 提供的系统架构的设计思路我觉得还是值得学习一下的,接下来的内容是我看完视频后的一点笔记. 架 ...

  7. java 导入Excel -- 套路及代码分析

    一.思路分析 1.我们要做导入,实际上也就是先文件上传,然后读取文件的数据. 2.我们要有一个导入的模板,因为我们导入的Excel列要和我们的数据字段匹配上,所以我们要给它来一个规定,也就是模板. 3 ...

  8. 将自己的框架更新到cocopods上

    为了更方便的集成第三方框架有了cocopods 的, 当我们有了相对比较好的框架的时候如何更新到cocopods 供他人参考呢? 下面我一步一步带大家开源自己的框架. 第一步 把自己的框架更新到git ...

  9. 胖AP基本配置:

      配置思路: 先创建wlan并广播ssid 进入射频子接口封装用户vlan 进入射频口关联wlan 注意:03 多个无线信号配置 注:在把AP改为 ap-mode fat后不能退出特权模式,不然需要 ...

  10. crm 03--->销售页面及逻辑

    基本思路 销售  ------->使用的是customer表来操作 有两个页面: 未成交的客户 公共客户页面  ---  什么属于?  --> 三天未跟进,及十五天内未成交 url    ...