代码:
string url="http://www.csdn.net/member/login.asp";
WebClient wc=new WebClient();
NameValueCollection nvc=new NameValueCollection();
nvc.Add("login_name","login_name");
nvc.Add("password","password");
byte[] responseArray = wc.UploadValues(url,"POST",nvc);
string content = Encoding.GetEncoding("GB2312").GetString(responseArray);问题:
为什么我提交后,仍然返回登陆页面,甚至都没有到提示出错的页面多谢

解决方案 »

  1.   

    用httpwebrequest和response类。给你个登录需要验证的网页例子:
    完整代码如下:
    --------------------------------------------------------------------
    //建立登录检查地址
    string url = "http://www.cn8815.com/member/checklogin.asp";
    //建立request对象
    System.Net.HttpWebRequest req=(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
    //这个新建的Cookie集合不知道有什么用??
    //req.CookieContainer =new CookieContainer ();

    req.Method="POST";//POST方式请求
    req.ContentType= "application/x-www-form-urlencoded";//内容类型
             
     
    //参数经过URL编码
    string paraUrlCoded;
    paraUrlCoded=System.Web.HttpUtility.UrlEncode("userid") +"="+System.Web.HttpUtility.UrlEncode("wanxl");
    paraUrlCoded+="&";
    paraUrlCoded+=System.Web.HttpUtility.UrlEncode("passwd")+"="+System.Web.HttpUtility.UrlEncode("999999");  //将URL编码后的字符串转化为字节
    byte[] payload;
    payload=System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
    req.ContentLength=payload.Length; //设置请求的ContentLength
    System.IO.Stream writer=req.GetRequestStream();//获得请求流
    writer.Write(payload,0,payload.Length);//将请求参数写入流
    writer.Close();//关闭请求流//获得响应流
    System.Net.HttpWebResponse response=(System.Net .HttpWebResponse)req.GetResponse();//获得响应Cookie
    System.Net.CookieCollection retCookie = response.cookies//获得流内容     
    System.IO.Stream s=response.GetResponseStream();
    StreamReader reader = new StreamReaders,System.Text .Encoding.Default);
    String respHTML = reader.ReadToEnd();
    //显示提示内容
     
    Label4.Text =respHTML;//如不成功,会显示登录失败3) 取得cookies 提交再一次请求,但发现cookie为null
    //请求URL地址
    string urlagain="http://www.cn8815.com/view.asp?id=20129&classname=配货需求";
    //生成请求
    HttpWebRequest reqagain=(HttpWebRequest)WebRequest.Create (url);
    //---注意,把上面得到的Cookie集合加入请求中
    reqagain.CookieContainer againCookie =new CookieContainer ();
    againCookie.Add(retCookie)
    // req.Timeout =5000;
     
    //下面就可以提交配货网页的请求了,代码略
      

  2.   


    http://www.codeproject.com/csharp/gmailagent.asp以编程方式登陆GMail,并处理邮件。登陆部分的代码可以拷贝来用。
      

  3.   

    CMIC(大象)
    用你的代码,访问http://www.cn8815.com/member/checklogin.asp能够得到预期的结果
    但是访问http://www.csdn.net/member/login.asp
    就会一直停留在登陆页面
    甚至连用户名密码错误的页面也见不到
      

  4.   

    daou101(海天一鸥)你给的代码和CMIC(大象)的基本一样
    都是通过httpwebrequest和response访问
    我的代码用WebClient,其实也因该没有错误我觉得好像是csdn的登陆页面有什么不同,麻烦哪位大侠帮忙分析分析
      

  5.   

    确定那些键/值对正确?
    另外现在CSDN还有其他的登陆选项,比如cookie保存时间等等
    都加上去看看这个又不是ASP.NET不需要处理viewstate
    应该没问题的
      

  6.   

    下一个http监视工具,看登录CSDN时想服务器post了什么信息和返回的cookie
      

  7.   

    多谢CMIC(大象) 赫赫
    我才知道原来还有这么好用的工具阿
    哈哈
    揭帖又,我的问题是 网址写错了:(
    url="http://www.csdn.net/member/logon.asp";
      

  8.   

    有什么http监视工具?介绍一下
      

  9.   

    那么自动登陆到asp.net的页面上呢!?