package org.sunnysolong.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* Wap登录返回测试
* Title: JavaTech<br>
* Description: <br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Nov 14, 2011 8:55:24 PM <br>
* @author wangmeng
*/
public class WapQuery {
private static String URL_LINK = "http://192.168.1.147/t3/wap/index.php";
// http://192.168.1.147/t3/wap/index.php?mod=topic&code=myhome
// 转向URL:http://192.168.1.147:80/t3/wap/index.php
private static final HttpClient HTTP_CLIENT = new HttpClient();
private static Cookie[] cookies;
private static GetMethod login;
private static GetMethod home;
/**
* 输入流转换成字符串
* @param is: 输入流
* @return 字符串对象
*/
private static String InputStreamToString(InputStream is){
BufferedReader reader = null;
StringBuffer responseText = null;
String readerText = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
responseText = new StringBuffer();
readerText = reader.readLine();
while(readerText != null){
responseText.append(readerText);
responseText.append(System.getProperty("line.separator"));
readerText = reader.readLine();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseText.toString();
}
/**
* 将cookie写入指定文件
* @param cookies: cookie
* @param fileName: 文件名
*/
private static void write(Cookie[] cookies, String fileName){
try {
String path = System.getProperty("user.home") + "\\" + fileName;
File file = new File(path);
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for(Cookie c : cookies){
bw.append(c.toString());
bw.append(System.getProperty("line.separator"));
}
bw.flush();
bw.close();
Runtime.getRuntime().exec("explorer " + path + "");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 登录
*/
private static void login(){
try {
//指定链接设定请求方式
login = new GetMethod(URL_LINK);
//入参
NameValuePair mod = new NameValuePair("mod", "login");
NameValuePair code = new NameValuePair("code", "dologin");
NameValuePair username = new NameValuePair("username", URLEncoder.encode("wangli@qq.com", "UTF-8"));
NameValuePair password = new NameValuePair("password", URLEncoder.encode("123456", "UTF-8"));
NameValuePair iswap = new NameValuePair("iswap", "1");
NameValuePair cityID = new NameValuePair("cityID", URLEncoder.encode("755", "UTF-8"));
NameValuePair city = new NameValuePair("city", URLEncoder.encode("深圳", "UTF-8"));
NameValuePair[] params = new NameValuePair[]{mod, code, username, password, iswap, cityID, city};
//设参
login.setQueryString(params);
//禁用重定向
login.setFollowRedirects(false);
//执行请求并返回状态码
int statusCode = HTTP_CLIENT.executeMethod(login);
//转向URL:http://192.168.1.147:80/t3/wap/index.php
if (statusCode != HttpStatus.SC_OK) {
System.err.println("LOGIN_ERROR >>> " + login.getStatusLine());
}
System.out.println("LOGIN_RESPONSE >>>" + InputStreamToString(login.getResponseBodyAsStream()));
//设定全局cookie
cookies = HTTP_CLIENT.getState().getCookies();
//记录cookie
write(cookies, "login-cookie.txt");
} catch (UnsupportedEncodingException e) {
System.err.println("(LOGIN) " + e.getMessage());
} catch (HttpException e) {
System.err.println("(LOGIN) " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("(LOGIN) " + e.getMessage());
} finally{
//释放连接
login.releaseConnection();
}
}
/**
* 主页
*/
private static void home(){
try {
//指定链接设定请求方式
home = new GetMethod(URL_LINK);
//入参、设参
home.setQueryString("mod=topic&code=myhome");
//记录cookie
write(HTTP_CLIENT.getState().getCookies(), "home-cookie.txt");
//添加cookie
HTTP_CLIENT.getState().addCookies(cookies);
//执行请求并返回状态码
int statusCode = HTTP_CLIENT.executeMethod(home);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("HOME_ERROR >>>" + home.getStatusLine());
}
//再次记录cookie
write(HTTP_CLIENT.getState().getCookies(), "access-cookie.txt");
System.out.println("HOME_RESPONSE >>>" + InputStreamToString(home.getResponseBodyAsStream()));
} catch (HttpException e) {
System.err.println("(HOME) " + e.getMessage());
} catch (IOException e) {
System.err.println("(HOME) " + e.getMessage());
} finally {
home.releaseConnection();
}
}
public static void main(String[] args) {
//登录
login();
//转向主页
home();
/**
* 说明:查看login-cookie.txt和home-cookie.txt中记录的cookie是否完全一致,是则表示会话信息保留完整。
* access-cookie.txt中记录的是第二次请求返回的cookie,用来与全局记录的cookie做信息对比。
*/
}
}
分享到:
相关推荐
$httpClient->get($url, $data = null, $cookie = null);var_dump($httpClient->buffer); 复制代码 代码如下:<?php class httpClient { public $buffer = null; // buffer 获取返回的字符串 public $referer =...
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1.创建HttpClient对象。 HttpClient client = new HttpClient(); 2.创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;...
排球曲奇使用截击保存 cookie 的示例1 - 将volley.jar从 libs 路径导入到您的项目2 - 在 build.gradle 依赖项中导入compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.3' 3...
做了一个android网络应用,要求用自己实现的webview去访问web网站,并且... DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(http://www.hlovey.com); HttpResponse respons
4、IHttpClient 和 IHttpAgent 组件可通过 SetUseCookie() 方法设置是否开启 Cookie 功能 > HTTP Sync Client 通信组件: 1、Create_HP_HttpSyncClient 和 Create_HP_HttpsSyncClient 方法增加可选参数 pListener 2...
它基于Apache HttpClient。 ConnectionHandler的优点: 支持同步和异步调用。 支持许多配置选项,例如连接超时,使用cookie等。 使用API记录整个通信的简单机制。 所有请求和响应的参数都包括在内:标题,...
使用示例: LeetCodeHelper是一个帮助类,帮助使用者用于和LeetCode交互,具体的使用方法可以看test中的使用方法 实例化帮助类LeetCodeHelper:gear: 三个参数分别用来用于发起http请求,配置CookieSpec,保存Cookie...
添加、删除、保存和管理示例 使用ajax json和form append进行保存和文件上传管理 使用formData-typescript的传输进度显示功能 条码生成功能 使用 selenium chrome 进行网页抓取 使用http客户端下载(简单异步,...
简单的请求示例:String url = ...; Response resp = Requests.get(url).text(); // post 和其他方法 resp = Requests.post(url).text(); resp = Requests.head(url).text(); //读取Http Response ...
支持标准的Cookie策略, 区分domain, path. 事件注解去除不常用的功能, 提高性能. 数据库api简化提高性能, 达到和greenDao一致的性能. 图片绑定支持gif, webp; 支持圆角, 圆形, 方形等裁剪, 支持自动旋转... 使用...