读书人

post 实现 淘宝登陆 有关问题

发布时间: 2013-07-20 11:07:48 作者: rapoo

求助 post 实现 淘宝登陆 问题

    public string login()
{
string m_Name = "";//用户名
string m_Password = "";//密码
string url = "http://login.taobao.com/member/login.jhtml";
string indata = "action=Authenticator&CtrlVersion=1,0,0,7&event_submit_do_login=anything&fc=2&from=tb&gvfdcname=10&gvfdcre=687474703A2F2F7777772E74616F62616F2E636F6D2F&loginType=3&longLogin=-1&pstrong=2&style=default&support=000001&tid=XOR_1_000000000000000000000000000000_675843554D0B09747B737579&TPL_username="+m_Name+"&TPL_password="+m_Password+"&TPL_redirect_url=";
return HttpRequestPost(url, indata);
}
public CookieContainer mycookie=new CookieContainer();
string str = "";
public string HttpRequestPost(string url, string indata)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = indata.Length;
request.Method = "POST";
request.Timeout = 3 * 60000;
request.CookieContainer = mycookie;
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*";
Stream requestStream = null;
StreamWriter writer = null;

requestStream = request.GetRequestStream();//获取请求数据流句柄
writer = new StreamWriter(requestStream, Encoding.GetEncoding("gbk"));


writer.Write(indata);//发送数据indata
writer.Close();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

mycookie.Add(response.Cookies);
Stream responseStream = null;
StreamReader reader = null;

responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream, Encoding.GetEncoding("gbk"));
str = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
request.Abort();
request = null;

return str;
}





求助 为什么每次执行 总会跳到登陆页面 貌似是登陆的时候没保存COOKIE 跪求解决方案!!
[解决办法]
public void alimamapost()
{

CookieContainer cc = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "action=MembersAction&event_submit_do_login=true&forward=&query_string=&_fmm.l._0.l=8824766@qq.com&originalLogpasswd=a8824766&_fmm.l._0.lo=c781f1ee5ab17dff12ff6019325688f2&dologin=";

byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://www.alimama.com/membersvc/member/login.htm");

myRequest.CookieContainer = cc;
myRequest.ContentType = "application/x-www-form-urlencoded";


myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
myRequest.Referer = "http://www.alimama.com/membersvc/member/login.htm";

myRequest.AllowAutoRedirect = true;
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*";
myRequest.ContentLength = data.Length;
myRequest.Timeout = 10000;
myRequest.Method = "POST";

Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

HttpWebResponse oresponse;

try
{
oresponse = (HttpWebResponse)myRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Response != null)
{
oresponse = (HttpWebResponse)ex.Response;
}
else
{
throw ex;
}
}

Stream dataStream = oresponse.GetResponseStream();

try
{

StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));

string responseFromServer = reader.ReadToEnd();

reader.Close();
dataStream.Close();
oresponse.Close();

}
catch (Exception k1)
{
throw k1;
}

myRequest =
(HttpWebRequest)WebRequest.Create("http://www.alimama.com/membersvc/adzone/site.htm?addsite=true");


myRequest.CookieContainer = cc;
try
{
oresponse = (HttpWebResponse)myRequest.GetResponse();
}
catch (WebException ex)
{
if (ex.Response != null)
{
oresponse = (HttpWebResponse)ex.Response;
}
else
{
throw ex;
}
}

dataStream = oresponse.GetResponseStream();



try
{

StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));

string responseFromServer = reader.ReadToEnd();

reader.Close();
dataStream.Close();
oresponse.Close();

}
catch (Exception k1)
{
throw k1;
}




}


这个是阿里妈妈登录的,你对照看一下
[解决办法]
登录这块,主要是如何获取COOKIE及将cookie发回去

读书人网 >C#

热点推荐