我用asp.net模拟登陆微信公共平台后台
返回值{
"Ret": 302,
"ErrMsg": "/cgi-bin/indexpage?t=wxm-index&lang=zh_CN&token=2094957214",
"ShowVerifyCode": 0,
"ErrCode": 0
}
说明已经是登陆成功了但是我保存不了cookie哪位大神可以帮个忙以下是我代码
string outdata = "";ASP.NET 微信 HttpWebRequest Cookie URL --------------------编程问答-------------------- 自己顶下,快来人啊 --------------------编程问答-------------------- 没弄过,帮顶下 --------------------编程问答-------------------- 用 myCookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri) 这个应该可以取出来保存。 --------------------编程问答-------------------- string Cookie= myHttpWebResponse.Headers["Set-Cookie"]; --------------------编程问答-------------------- 亲,可以发一份你的微信模拟登陆我吗。。361337917@qq.com,有bug也行 --------------------编程问答-------------------- /// <summary>
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata, 0, indata.Length);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myCookieContainer.Add(myHttpWebResponse.Cookies);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
ServiceMVC.Model.WeiXinResponse _model = new ServiceMVC.Model.WeiXinResponse();
_model = js.Deserialize<ServiceMVC.Model.WeiXinResponse>(outdata);
string strErrMsg =_model.ErrMsg;
TOKEN = strErrMsg.Substring(strErrMsg.LastIndexOf("=")+1);
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
// Console.WriteLine(outdata);
//显示"登录"
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myCookieContainer.Add(myHttpWebResponse.Cookies);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
/// cookie值
/// </summary>
private static CookieContainer _mCookieContainer = new CookieContainer();
public static CookieContainer cookieContainer
{
set { _mCookieContainer = value; }
get { return _mCookieContainer; }
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.AllowAutoRedirect = true;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
requestStream = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);
requestStream.Write(param);
requestStream.Close();
request.CookieContainer = new CookieContainer();
response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
cookieContainer = request.CookieContainer; //保存cookies
return responseStr;
微信需要增加
request.CookieContainer = new CookieContainer();
request.CookieContainer = cookies;
还有request.Referer --------------------编程问答-------------------- 可以发一份给我吗,谢谢,谢谢。拜托,拜托!!!1014161003@qq.com --------------------编程问答-------------------- 亲 有代码没呢..给我发一份吧..我也关注一下 1101350716@qq.com --------------------编程问答-------------------- 我也要,发一分给我57871085@qq.com --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 你是怎么成功的 我复制你代码返回-2啊 --------------------编程问答-------------------- 如果成功,可以发一份给我吗,谢谢,谢谢。拜托,拜托!!!949520321@qq.com --------------------编程问答-------------------- 求代码515114557@qq.com谢谢 --------------------编程问答-------------------- 楼主解决了吗 --------------------编程问答-------------------- 294787732@qq.com 我也想参考下,谢谢 --------------------编程问答-------------------- 楼主,解决了没。我也 想求一份代码。webcilisky@126.com --------------------编程问答-------------------- 提交的登陆数据如何写?格式是怎样的?
补充:.NET技术 , ASP.NET