wpf(c#)开发的客户端,想要实现打开浏览器并登录网站的功能。参考了这个 http://blog.csdn.net/htsnoopy/article/details/7094224
我通过c#代码生成了一个cookie,但是打开浏览器跳转网页的时候发现session里面的值是空的。
仔细研究了一下才发现,phpthink框架开发的网站每次都会生成固定的sessionid,而我的代码每次生成的sessionid都是随机的,对不上号所以也就登录不了,怎么办呢?
回复讨论(解决方案) sessionid 是服务端产生的,客户端只能读取
不然如何能沟通呢?
sessionid 是服务端产生的,客户端只能读取
不然如何能沟通呢? 嗯 这个我知道 服务端也在本地 我不知道该怎么设置才能让他们一致啊
sessionid 是服务端产生的,客户端只能读取
不然如何能沟通呢? 服务器是用wamp搭建的
你在客户端读呀!
你给的链接里不是有读取 cookie 的部分吗?
还要把读到的 cookie 发回去,他也应该是有的
你在客户端读呀!
你给的链接里不是有读取 cookie 的部分吗? 读到了,也发回去了,但是最后打开网页的时候,他又重新生成了一个session,这个sessionid和我发的不一样,而且无论怎么弄她都是固定的id
那你的 php 部分是怎么写的?
如果“无论怎么弄她都是固定的id”,那你读到的和发回去的不也是那个固定的id吗?
那你的 php 部分是怎么写的?
如果“无论怎么弄她都是固定的id”,那你读到的和发回去的不也是那个固定的id吗? 不是哦 什么原因呢?
你还是贴代码吧!
c#获取cookie登录部分
httpheader header = new httpheader(); header.accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8; header.contenttype = application/x-www-form-urlencoded; header.method = post; header.useragent = mozilla/4.0 (compatible; msie 8.0; windows nt 6.1; wow64; trident/4.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; infopath.3; .net4.0c; .net4.0e; header.maxtry = 200; cookiecollection mycookie = htmlhelper.getcookiecollection(http://localhost/gyjw/index.php/index/login_into, admin=admin&password=424, header); foreach (cookie cookie in mycookie) //将cookie设置为浏览的cookie { internetsetcookie( http:// + cookie.domain.tostring(), cookie.name.tostring(), cookie.value.tostring() + ;expires=sun,22-feb-2099 00:00:00 gmt); } //system.diagnostics.process.start(http://localhost/gyjw/index.php/index/login.html); //system.diagnostics.process.start(http://localhost/gyjw/index.php/index/login_into); //system.diagnostics.process.start(http://localhost/gyjw/index.php); system.diagnostics.process.start(http://localhost/gyjw/index.php/user/adduser.html);
httphelper类
using system;using system.collections.generic;using system.linq;using system.text;using system.net;using system.io;using system.threading;namespace wpfui{ public class htmlhelper { /// /// 获取cookie /// /// /// /// /// public static cookiecontainer getcookie(string loginurl, string postdata, httpheader header) { httpwebrequest request = null; httpwebresponse response = null; try { cookiecontainer cc = new cookiecontainer(); request = (httpwebrequest)webrequest.create(loginurl); request.method = header.method; request.contenttype = header.contenttype; byte[] postdatabyte = encoding.utf8.getbytes(postdata); request.contentlength = postdatabyte.length; request.allowautoredirect = false; request.cookiecontainer = cc; request.keepalive = true; //提交请求 stream stream; stream = request.getrequeststream(); stream.write(postdatabyte, 0, postdatabyte.length); stream.close(); //接收响应 response = (httpwebresponse)request.getresponse(); response.cookies = request.cookiecontainer.getcookies(request.requesturi); cookiecollection cook = response.cookies; //cookie字符串格式 string strcrook = request.cookiecontainer.getcookieheader(request.requesturi); return cc; } catch (exception ex) { throw ex; } } /// /// 获取cookiecollection /// /// /// /// /// public static cookiecollection getcookiecollection(string loginurl, string postdata, httpheader header) { httpwebrequest request = null; httpwebresponse response = null; try { cookiecontainer cc = new cookiecontainer(); request = (httpwebrequest)webrequest.create(loginurl); request.method = header.method; request.contenttype = header.contenttype; byte[] postdatabyte = encoding.utf8.getbytes(postdata); request.contentlength = postdatabyte.length; request.allowautoredirect = false; request.cookiecontainer = cc; request.keepalive = true; //提交请求 stream stream; stream = request.getrequeststream(); stream.write(postdatabyte, 0, postdatabyte.length); stream.close(); //接收响应 response = (httpwebresponse)request.getresponse(); response.cookies = request.cookiecontainer.getcookies(request.requesturi); cookiecollection cook = response.cookies; //cookie字符串格式 string strcrook = request.cookiecontainer.getcookieheader(request.requesturi); return cook; } catch (exception ex) { throw ex; } } /// /// 获取html /// /// /// /// /// public static string gethtml(string geturl, cookiecontainer cookiecontainer,httpheader header) { thread.sleep(1000); httpwebrequest httpwebrequest = null; httpwebresponse httpwebresponse = null; try { httpwebrequest = (httpwebrequest)httpwebrequest.create(geturl); httpwebrequest.cookiecontainer = cookiecontainer; httpwebrequest.contenttype = header.contenttype; httpwebrequest.servicepoint.connectionlimit = header.maxtry; httpwebrequest.referer = geturl; httpwebrequest.accept = header.accept; httpwebrequest.useragent = header.useragent; httpwebrequest.method = get; httpwebresponse = (httpwebresponse)httpwebrequest.getresponse(); stream responsestream = httpwebresponse.getresponsestream(); streamreader streamreader = new streamreader(responsestream, encoding.utf8); string html = streamreader.readtoend(); streamreader.close(); responsestream.close(); httpwebrequest.abort(); httpwebresponse.close(); return html; } catch (exception e) { if (httpwebrequest != null) httpwebrequest.abort(); if (httpwebresponse != null) httpwebresponse.close(); return string.empty; } } } public class httpheader { public string contenttype { get; set; } public string accept { get; set; } public string useragent { get; set; } public string method { get; set; } public int maxtry { get; set; } }}
登陆页面
后台登陆 系统
用户名
密 码
打印出你获取到的 cookie 的值
打印出你获取到的 cookie 的值
o8ru11js96qn9rneqokpc6alo2是这个吗?
打印出你获取到的 cookie 的值 guestname|s:5:admin;userid|n;这个?
说错了,要同时打印 cookie 变量名和值
tp 的表单有可能使用了 token,也要处理的
模拟登录的流程是:
1、访问登陆页,获取表单元素名和值(已知的话可跳过)
2、获取 cookie、获取可能存在的 token
3、获取可能存在的验证码图片并解析。如果有验证码,还需重新获取 cookie
4、发送应提交的数据集合和 cookie 到表单目标页
说错了,要同时打印 cookie 变量名和值
tp 的表单有可能使用了 token,也要处理的
模拟登录的流程是:
1、访问登陆页,获取表单元素名和值(已知的话可跳过)
2、获取 cookie、获取可能存在的 token
3、获取可能存在的验证码图片并解析。如果有验证码,还需重新获取 cookie
4、发送应提交的数据集合和 cookie 到表单目标页 admin:adminpassword:424__hash__:9dcee4d5bcfb44270f8cbfaebbc2d786_a57a03650a2256a207ed4f6ccaad6bf0
没有验证码,这个__hash__是您指的token吗?
应该是
你贴出的数据中并没有 sessionid
你怎么总是取检查 session 呢?方向错了
应该是
你贴出的数据中并没有 sessionid
你怎么总是取检查 session 呢?方向错了 o8ru11js96qn9rneqokpc6alo2这个是sessionid 但是是随机的 网站自己生成的sessionid是固定的。
哪个方向错了啊!大神多指点指点,不懂呢
如果有 session 那么他在 cookie 数据中表现为
phpsessid:.................
你打印出的数据中没有这样的内容
c#不懂,但你每次连接服务器服务器都会重新建立一个session(服务器端配置文件里面设置的?),但是你的客户端这边好像并没有再次接受这个sessionid,还是使用以前的sessionid,你在程序中写上当退出页面的时候删除cookie行不行呢
如果有 session 那么他在 cookie 数据中表现为
phpsessid:.................
你打印出的数据中没有这样的内容 谢谢大神,仔细思考了一下,我那些数据是通过chromef12跟踪到的formdata,具体什么是token,新手不懂,但是我感觉我获取的cookie没有这个数据,发送的cookie也没有,phpsessid在我的程序中就是cookie.value的值,这个是我跟踪调试时发现的,而且在我获取cookie的时候,服务器存session的文件夹里面已经有对应id的session了,里面的内容也存了admin,只是没有密码信息,可能是安全考虑。
下面我把session的内容发出来,您给看看,该怎么弄。
先发生成的固定session,sess_60k402oc795mpvihq9jodpnml7
guestname|s:5:admin;userid|n;__hash__|a:2:{s:32:29b296010dbae92683f9cfada6743386;s:32:f6f026587c241f4f2f6f94ed4e957ccb;s:32:d150a69b13ade312006133d91f4fc9b1;s:32:ef751c9ebc0066c0a98731b2b819ac98;}
再发随机生成的session,sess_ln3gn3acd5a6arme32h3viq3e5
guestname|s:5:admin;userid|n;
如果不打开客户端直接网站登录,那么只会生成固定的那个session;如果打开客户端登录,就会先生成一个随机的,当通过客户端打开浏览器登录时,又会生成一个固定的,此时固定的没有guestname信息。
其中guestname是用户名的信息,网站的一切操作权限都是关联的这个guestname,userid始终是n,不知道是什么意思,__hash__我感觉是操作生成的记录信息,因为每一个页面跳转操作都会刷新__hash__内容。
我的问题出在哪里呢?
c#不懂,但你每次连接服务器服务器都会重新建立一个session(服务器端配置文件里面设置的?),但是你的客户端这边好像并没有再次接受这个sessionid,还是使用以前的sessionid,你在程序中写上当退出页面的时候删除cookie行不行呢 之前的php配置文件我从来没动过,后来修改了一下,不好使然后又改了回去。。。删除cookie肯定也是不行的撒,那个固定的cookie根本就没有用户的信息。
没改的话php中的session是概率刷新的,默认只有千分之一,现在phpthink那边每次都会随机产生,估计配置文件里面设置了每次重新打开网站session百分之百刷新
你打开php.ini 看一下session.gc_probability和session.gc_divisor的值分别是多少,这两个值影响session的刷新概率
现在你估计只要把里面的session.gc_probability设为0就能保证session不刷新
没改的话php中的session是概率刷新的,默认只有千分之一,现在phpthink那边每次都会随机产生,估计配置文件里面设置了每次重新打开网站session百分之百刷新
你打开php.ini 看一下session.gc_probability和session.gc_divisor的值分别是多少,这两个值影响session的刷新概率
现在你估计只要把里面的session.gc_probability设为0就能保证session不刷新 没效果……看来是程序的问题
sessionid 为 60k402oc795mpvihq9jodpnml7 时
生成的临时文件为 sess_60k402oc795mpvihq9jodpnml7
至于里面是什么内容,则是有你的程序决定的,与旁人无关
此时 cookie 中会有 phpsessid:60k402oc795mpvihq9jodpnml7 项
如果你的客户端程序没有读到他,就表示你的客户端程序有问题
鉴于你的客户端是 c# 的,你应该到 .net 版面去求解,显然那边 c# 的水平高于这里
sessionid 为 60k402oc795mpvihq9jodpnml7 时
生成的临时文件为 sess_60k402oc795mpvihq9jodpnml7
至于里面是什么内容,则是有你的程序决定的,与旁人无关
此时 cookie 中会有 phpsessid:60k402oc795mpvihq9jodpnml7 项
如果你的客户端程序没有读到他,就表示你的客户端程序有问题
鉴于你的客户端是 c# 的,你应该到 .net 版面去求解,显然那边 c# 的水平高于这里 好的 谢谢 经过您的讲解 我也觉得是程序的问题
你用浏览器去访问你的登陆页面,如果不出问题,则一定是你的客户端有问题了
你用浏览器去访问你的登陆页面,如果不出问题,则一定是你的客户端有问题了 是的 php后台登录是没问题的,只是对phpthink一点都不懂,所以才来这边问问。
既然用浏览器没有问题,显然浏览器也不会懂 phpthink 的
你的客户端只是模拟了浏览器的行为,自然也不需要懂 phpthink
既然用浏览器没有问题,显然浏览器也不会懂 phpthink 的
你的客户端只是模拟了浏览器的行为,自然也不需要懂 phpthink 那phpthink是怎么运行的?浏览器只懂html吗?
是的,浏览器只懂html
嗯,还懂 js