我试图登录到一个网站,并保持该会话/饼干,这样服务器会承认我的登录信息,但我在努力找出从响应提取cookie,并设置成一个请求,维持的一种方式我登录。我不知道我是否应该去考虑的标题设置Cookie或使用CookieStore的。任何帮助是极大的AP preciated。这是我的code,我有,有,我觉得getHeader /的getCookie方法会去评论。
公共类的Http
{
DefaultHttpClient客户端=新DefaultHttpClient();
HTTPGET请求;
HttpEntity实体;
HTT presponse响应;
HttpPost岗位;
CookieStore的CookieStore的=新BasicCookieStore();
HttpContext的localContext =新BasicHttpContext();
公共静态无效setContext()
{
localContext.setAttribute(ClientContext.COOKIE_STORE,CookieStore的);
}
公共静态无效GETPAGE(字符串URL)抛出异常
{
要求=新HTTPGET(URL);
响应= client.execute(请求,localContext);
。PARSER preParse(URL,响应);
}
公共静态的Htt presponse postPage(名单<的NameValuePair>参数,可以字符串主机,串动)抛出异常
{
后=新HttpPost(主机+动作);
post.setEntity(新UrlEn codedFormEntity(参数,可以HTTP.UTF_8));
响应= client.execute(后,localContext);
实体= response.getEntity();
如果(实体!= NULL)
{
entity.consumeContent();
}
返回响应;
}
公共无效destoyHttp()
{
client.getConnectionManager()关闭()。
}
}
在别人更好地理解我的困惑,希望我加入code,我知道不工作,并保持会话,但是当我试图将code到我的实际应用就坏在这条线。
公共类HttpClientTest延伸活动{
DefaultHttpClient客户端=新DefaultHttpClient();
HTTPGET请求;
HttpEntity实体;
名单<饼干>曲奇饼;
HTT presponse响应;
HttpPost岗位;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
尝试 {
调用getRequest();
}赶上(例外五){
Log.d(我的活动,失败);
e.printStackTrace();
}
}
公共无效调用getRequest()抛出异常
{
最终的字符串变量=MyActivity;
要求=新HTTPGET(http://gc.gamestotal.com/i.cfm?p=login&se=4);
响应= client.execute(要求);
串动=i.cfm和放大器; 1028&安培; P =登录和放大器; SE = 4;
字符串yourServer =http://gc.gamestotal.com/;
后=新HttpPost(yourServer +动作);
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(NIC,名为myusername));
params.add(新BasicNameValuePair(密码,输入mypassword));
params.add(新BasicNameValuePair(服务器,4));
post.setEntity(新UrlEn codedFormEntity(参数,可以HTTP.UTF_8));
响应= client.execute(后);
实体= response.getEntity();
如果(实体!= NULL){
entity.consumeContent();
}
要求=新HTTPGET(http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3);
响应= client.execute(要求);
如果(实体!= NULL)
{
entity.consumeContent();
}
}
}
我知道它的工作原理其实你可以看到,在这个code我consumeContent(),但补充说顶端code似乎没有有所作为,所以我离开了出来。任何想法?
编辑:我仍然无法得到code工作在保持会话持续。我从我目前的code发布的饼干,随着第二code,它是工作的cookie。也许有人会注意到一个问题,我当然不知道。
6月八日至31日:53:50.318:VERBOSE / SFGC(496): - [版本:0] [名称:CFID] [值:26651316] [域:gc.gamestotal .COM] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:50.329:VERBOSE / SFGC(496): - [版本:0] [名称:CFTOKEN] [值:96917381] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:50.329:VERBOSE / SFGC(496): - [版本:0] [名称:CFCLIENT_SFGC_106Y] [值: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:50.339:VERBOSE / SFGC(496): - [版本:0] [名称:CFGLOBALS] [值: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:51.938:VERBOSE / SFGC(496): - [版本:0] [名称:CFID] [值:26651316] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:51.938:VERBOSE / SFGC(496): - [版本:0] [名称:CFTOKEN] [值:96917381] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:51.938:VERBOSE / SFGC(496): - [版本:0] [名称:CFCLIENT_SFGC_106Y] [值: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:53:51.948:VERBOSE / SFGC(496): - [版本:0] [名称:CFGLOBALS] [值: HITCOUNT%3D3%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A46%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:41.628:DEBUG / MyActivity(469): - [版本:0] [名称:CFID] [值:26651274] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:41.638:DEBUG / MyActivity(469): - [版本:0] [名称:CFTOKEN] [值:58361320] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:41.638:DEBUG / MyActivity(469): - [版本:0] [名称:CFCLIENT_SFGC_106Y] [值: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:41.648:DEBUG / MyActivity(469): - [版本:0] [名称:CFGLOBALS] [值: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:44.138:DEBUG / MyActivity(469): - [版本:0] [名称:CFID] [值:26651274] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:44.138:DEBUG / MyActivity(469): - [版本:0] [名称:CFTOKEN] [值:58361320] [域:gc.gamestotal.com] [路径:/] [到期:太阳09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:44.138:DEBUG / MyActivity(469): - [版本:0] [名称:CFCLIENT_SFGC_106Y] [值: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
六月8日至31日:52:44.138:DEBUG / MyActivity(469): - [版本:0] [名称:CFGLOBALS] [值: HITCOUNT%3D4%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A38%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com] [路径:/] [到期:孙年09月27日00:00:00 GMT + 00:00 2037]
解决方案
我无法得到我自己的code工作(我可能会工作再次更新版本),但我发现有用的code此处http://stackoverflow.com/questions/874227/android-project-using-httpclient-http-client-apache-post-get-method而且我在使用由查理·柯林斯,这是类似的Http code在ZXing Android的例子建类。我最终可能会移动到ZXing code。
I am trying to login to a site and maintain that session/cookie so that the server will recognize my login, but I am struggling to figure out a way of extracting the cookie from the response and setting into a request to maintain my login. I'm wondering if I should go about taking the header "Set-Cookie" or using a CookieStore. Any help is greatly appreciated. Here is my code that I have, with comments where I think the getHeader/getCookie methods would go.
public class Http
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request;
HttpEntity entity;
HttpResponse response;
HttpPost post;
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
public static void setContext()
{
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
public static void getPage(String url) throws Exception
{
request = new HttpGet(url);
response = client.execute(request, localContext);
PARSER.preParse(url, response);
}
public static HttpResponse postPage(List<NameValuePair> params, String host, String action) throws Exception
{
post = new HttpPost(host + action);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
response = client.execute(post, localContext);
entity = response.getEntity();
if(entity != null)
{
entity.consumeContent();
}
return response;
}
public void destoyHttp()
{
client.getConnectionManager().shutdown();
}
}
In hopes of others better understanding my confusion am adding code that I know DOES work and maintains a session but when I tried to move the code into my actual application it broke somewhere down the line.
public class HttpClientTest extends Activity{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request;
HttpEntity entity;
List<Cookie> cookies;
HttpResponse response;
HttpPost post;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
getRequest();
} catch (Exception e) {
Log.d("My Activity", "Failed");
e.printStackTrace();
}
}
public void getRequest() throws Exception
{
final String TAG = "MyActivity";
request = new HttpGet("http://gc.gamestotal.com/i.cfm?p=login&se=4");
response = client.execute(request);
String action = "i.cfm?&1028&p=login&se=4";
String yourServer = "http://gc.gamestotal.com/";
post = new HttpPost(yourServer + action);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nic", "myusername"));
params.add(new BasicNameValuePair("password", "mypassword"));
params.add(new BasicNameValuePair("server", "4"));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
response = client.execute(post);
entity = response.getEntity();
if(entity != null){
entity.consumeContent();
}
request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3");
response = client.execute(request);
if(entity != null)
{
entity.consumeContent();
}
}
}
I know for a fact it works, and you can see that in this code I consumeContent() but adding that to the top code didn't seem to make a difference so I left it out. Any Ideas?
Edit: I am still unable to get the code working in keeping a session going. I am posting the cookies from my current code, along with the cookies of the second code that is working. Perhaps someone will notice an issue, I certainly do not.
08-31 06:53:50.318: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:50.339: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:53:51.948: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D3%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A46%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:41.628: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:41.648: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D4%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A38%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
解决方案
I was unable to get my own code working (I might work on it again later), but I found useful code here http://stackoverflow.com/questions/874227/android-project-using-httpclient-http-client-apache-post-get-method and I am using the class built by Charlie Collins, which is similar to the Http Code in the ZXing Android example. I may eventually move to the ZXing code.