本文整理匯總了Java中org.apache.commons.httpclient.HttpState.addCookies方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpState.addCookies方法的具體用法?Java HttpState.addCookies怎麽用?Java HttpState.addCookies使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.HttpState
的用法示例。
在下文中一共展示了HttpState.addCookies方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testParseMultipleDifferentPaths
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testParseMultipleDifferentPaths() throws Exception {
Header header = new Header("Set-Cookie",
"name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
"Path=/commons/httpclient;Version=1");
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
HttpState state = new HttpState();
state.addCookies(parsed);
Cookie[] cookies = state.getCookies();
assertEquals("Wrong number of cookies.",2,cookies.length);
assertEquals("Name","name1",cookies[0].getName());
assertEquals("Value","value1",cookies[0].getValue());
assertEquals("Name","name1",cookies[1].getName());
assertEquals("Value","value2",cookies[1].getValue());
}
示例2: testParseMultipleSamePaths
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testParseMultipleSamePaths() throws Exception {
Header header = new Header("Set-Cookie",
"name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
HttpState state = new HttpState();
state.addCookies(parsed);
Cookie[] cookies = state.getCookies();
assertEquals("Found 1 cookies.",1,cookies.length);
assertEquals("Name","name1",cookies[0].getName());
assertEquals("Value","value2",cookies[0].getValue());
}
示例3: request
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public static int request(HttpMethodBase httpMethod, String host, int port, String protocal, Cookie[] cookies)
throws HttpException, IOException {
HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getHttpConnectionManager().getParams().setConnectionTimeout(4000);
client.getHostConfiguration().setHost(host, port == 0 ? 80 : port, protocal == null ? "http" : protocal);
if(cookies != null){
HttpState state = new HttpState();
state.addCookies(cookies);
client.setState(state);
}
return client.executeMethod(httpMethod);
}
示例4: login
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public static HashMap<String, Object> login(String username,String password) throws HttpException, IOException,UnknownHostException, JSONException{
HashMap<String, Object> map = new HashMap<String, Object>();
PostMethod post = new PostMethod("/login.aspx");
NameValuePair[] nps = new NameValuePair[2];
nps[0] = new NameValuePair("username", username);
nps[1] = new NameValuePair("password", password);
post.setRequestBody(nps);
post.setRequestHeader("Content-Type", Connectivity.CONTENT_TYPE_FORM);
HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getHostConfiguration().setHost("www.acfun.tv", 80, "http");
int state = client.executeMethod(post);
if(state>200){
map.put("success", false);
map.put("result", "ac娘大姨媽?");
}else{
JSONObject re = new JSONObject(post.getResponseBodyAsString());
if(re.getBoolean("success")){
Cookie[] cks = client.getState().getCookies();
map.put("Cookies", cks);
PostMethod mempost = new PostMethod("/user_check.aspx");
HttpState localHttpState = new HttpState();
localHttpState.addCookies(cks);
client.setState(localHttpState);
client.executeMethod(mempost);
String jsonstring = mempost.getResponseBodyAsString();
JSONObject job = new JSONObject(jsonstring);
String uname = job.getString("uname");
String signature = job.get("signature").toString();
String avatar = job.getString("avatar");
int uid = job.getInt("uid");
map.put("uname", uname);
map.put("signature", signature);
map.put("avatar", avatar);
map.put("uid", uid);
map.put("success", true);
}else{
map.put("success", false);
map.put("result", re.get("result"));
}
}
return map;
}
示例5: login
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public static HashMap<String, Object> login(String host, String username, String password) throws HttpException, IOException,
UnknownHostException, JSONException {
HashMap<String, Object> map = new HashMap<String, Object>();
PostMethod post = new PostMethod("/login.aspx");
NameValuePair[] nps = new NameValuePair[2];
nps[0] = new NameValuePair("username", username);
nps[1] = new NameValuePair("password", password);
post.setRequestBody(nps);
post.setRequestHeader("Content-Type", Connectivity.CONTENT_TYPE_FORM);
HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.single-cookie-header", true);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getHostConfiguration().setHost(host, 80, "http");
int state = client.executeMethod(post);
if (state > 200) {
map.put("success", false);
map.put("result", "ac娘大姨媽?");
return map;
}
JSONObject re = JSON.parseObject(post.getResponseBodyAsString());
if (!re.getBoolean("success")) {
map.put("success", false);
map.put("result", re.get("result"));
return map;
}
Cookie[] cks = client.getState().getCookies();
String uid = "";
String avatar = "";
String uname = "";
String signature = "";
for (Cookie ck : cks) {
if (ck.getName().equals("auth_key")) {
uid = ck.getValue();
} else if (ck.getName().equals("ac_username")) {
uname = URLDecoder.decode(ck.getValue(), "UTF-8");
} else if (ck.getName().equals("ac_userimg")) {
avatar = URLDecoder.decode(ck.getValue(), "UTF-8");
}
}
if(TextUtils.isEmpty(uid)){
map.put("success", false);
map.put("result", "登錄失敗");
return map;
}
GetMethod getInfo = new GetMethod("/usercard.aspx?uid=" + uid);
HttpState localHttpState = new HttpState();
localHttpState.addCookies(cks);
client.setState(localHttpState);
client.executeMethod(getInfo);
String jsonstring = getInfo.getResponseBodyAsString();
try {
JSONObject job = JSON.parseObject(jsonstring);
if (job.getBoolean("success")) {
JSONObject userjson = job.getJSONObject("userjson");
signature = userjson.getString("sign");
uname = userjson.getString("name");
}
} catch (Exception e) {
map.put("success", false);
map.put("result", "登錄失敗");
return map;
}
User user = new User(Integer.parseInt(uid), uname, avatar, signature);
user.cookies = JSON.toJSONString(cks, false);
map.put("user", user);
map.put("success", true);
return map;
}
示例6: post
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public PostMethod post(HttpUrl httpUrl,Cookie[] cookies) throws Exception {
httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
HttpState initialState = new HttpState();
initialState.addCookies(cookies);
httpClient.setState(initialState);
return this.post(httpUrl);
}