高手帮我测试下吧,我需要通过程序来登录网页
地址是:http://www.inang.cn/blog/login.asp
用户名:test
密码:testtest代码如下,为什么手工可以登录,代码却登录不了
string uriString = "http://www.inang.cn/blog/login.asp?act=login";
string postString = "userName=test&password=testtest&Login=Login&CookiesDay=1";WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = Encoding.ASCII.GetBytes(postString);
byte[] responseData = webClient.UploadData(uriString, "POST", postData);
string srcString = Encoding.UTF8.GetString(responseData);

解决方案 »

  1.   

    tring url = "http://my.b2b.hc360.com/my/turbine/template/firstview,other_login.html";string indata = @"LoginID=mytestcs&Passwd=aabbccdd&LoginChk=true&Submit=%B5%C7%A1%A1%A1%A1%C2%BC";            string outdata="";             CookieContainer myCookieContainer=new CookieContainer(); 
                //新建一个CookieContainer来存放Cookie集合 
                HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url); 
                //新建一个HttpWebRequest 
                myHttpWebRequest.ContentType="application/x-www-form-urlencoded"; 
                myHttpWebRequest.ContentLength=indata.Length; 
                myHttpWebRequest.Method="POST"; 
                myHttpWebRequest.CookieContainer=myCookieContainer; 
                //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer 
                Stream myRequestStream=myHttpWebRequest.GetRequestStream(); 
                StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));                 
                myStreamWriter.Write(indata); 
                //把数据写入HttpWebRequest的Request流 
                myStreamWriter.Close(); 
                myRequestStream.Close();             //关闭打开对象 
                HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 
                //新建一个HttpWebResponse 
                myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); 
                //获取一个包含url的Cookie集合的CookieCollection 
                Stream myResponseStream=myHttpWebResponse.GetResponseStream(); 
                StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312")); 
                outdata=myStreamReader.ReadToEnd(); 
                //把数据从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);             
                myResponseStream=myHttpWebResponse.GetResponseStream(); 
                myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312")); 
                outdata=myStreamReader.ReadToEnd(); 
                myStreamReader.Close(); 
                myResponseStream.Close(); 
                Console.WriteLine(outdata);
      

  2.   

    如何给网页Post数据
    ///////////////////////////////////////////////////////////////////////////   
      //SDK   post   
      ///////////////////////////////////////////////////////////////////////////   
      #include   "stdafx.h"   
      #include   "winsock.h"   
      #pragma   comment(lib,"ws2_32.lib")   
      #define   winsock_version   0x0101   
      void   main()   
      {   
      //I   create     C:\Inetpub\wwwroot\test\test.asp   ,start   the   web   service   
      //start   my   program,   the   result   is   OK.   
      //If   it   works,it   is   written   by   masterz,otherwise   I   don't   know   who   write   it.   
              SOCKADDR_IN   saServer;   
      LPHOSTENT   lphostent;   
      WSADATA   wsadata;   
              SOCKET   hsocket;   
      int   nRet;   
      const   char*   host_name="127.0.0.1";   
      char*   req="POST   /test/test.asp   HTTP/1.0\r\n"   
      "From:   local\r\n"   
      "User-Agent:   post_test/1.0\r\n"   
      "Content-Type:   application/x-www-form-urlencoded\r\n"   
      "Content-Length:   20\r\n\r\n"   
      "type=12345&name=aaaa";   
      if(WSAStartup(winsock_version,&wsadata))   
      printf("can't   initial   socket");   
              lphostent=gethostbyname(host_name);   
              if(lphostent==NULL)   
      printf("lphostent   is   null");   
      hsocket   =   socket(AF_INET,   SOCK_STREAM,   IPPROTO_TCP);   
              saServer.sin_family   =   AF_INET;   
      //   Use   def.   now,   need   to   handle   general   case   
      saServer.sin_port   =   htons(80);   
      saServer.sin_addr   =   *((LPIN_ADDR)*lphostent->h_addr_list);   
              nRet   =   connect(hsocket,   (LPSOCKADDR)&saServer,   sizeof(SOCKADDR_IN));   
      if   (nRet   ==   SOCKET_ERROR)   
      {   
      printf("can't   connect");   
      closesocket(hsocket);   
      return;   
      }   
      else   
      printf("connected   with   %s\n",host_name);   
      nRet   =   send(hsocket,   req,   strlen(req),   0);   
      if   (nRet   ==   SOCKET_ERROR)   
      {   
      printf("send()   failed");   
      closesocket(hsocket);   
        
      }   
      else   
      printf("send()   OK\n");   
      char   dest[1000];   
      nRet=1;   
      while(nRet>0)   
      {   
      nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);   
      if(nRet>0)   
      dest[nRet]=0;   
      else   
      dest[0]=0;   
      printf("\nReceived   bytes:%d\n",nRet);   
      printf("Result:\n%s",dest);   
      }   
      }
    void   post()   
      {   
              CInternetSession   session("My   Session");   
              CHttpConnection*   pServer   =   NULL;   
              CHttpFile*   pFile   =   NULL;   
              CString   ServerName   =   "webmail.21cn.com";   
              INTERNET_PORT   nPort   =   80;   
              DWORD   retcode;   
              char   outBuff[300]   =   "LoginName=aaa&passwd=xxx&DomainName=21cn.com";//I   have   test   this   with   my   loginname   and   password   
              try   
              {   
                      pServer   =   session.GetHttpConnection(ServerName,nPort);   
                      pFile   =   pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/NULL/NULL/NULL/NULL/NULL/SignIn.gen",NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);   
                      pFile   ->   AddRequestHeaders("Content-Type:   application/x-www-form-urlencoded");   
                      pFile   ->   AddRequestHeaders("Accept:   */*");   
                      pFile   ->   SendRequest(NULL,0,outBuff,strlen(outBuff)+1);   
                      pFile   ->   QueryInfoStatusCode(retcode);   
                      //   you   can   read   from   the   file   after   this......I've   just   left   it   out.   
                      for(int   i=0;i<10;i++)//read   ten   line   of   returned   HTML,you   need   to   edit   this   to   use   the   result   
                      {   
                      CString   smsg;   
                      pFile->ReadString(smsg);   
                      AfxMessageBox(smsg);   
                      }   
      DWORD   dwlen=pFile->GetLength();   
      char   buf[1024];   
      DWORD   dwread=pFile->Read(buf,1024);   
              }   
              catch   (CInternetException   *   e){};   
              delete   pFile;   
              delete   pServer;   
              session.Close();   
      }
      

  3.   

    这个是提交的数据类:
     using System.Collections;
     using System.IO;
     
     public class RequestData
      {
     ArrayList arr=new ArrayList();
     public RequestData()
      {
     
     }
     
     public string GetData()
      {
     string r="";
     
     for(int i=0;i<arr.Count;i++)
      {
     data d=(data)arr[i];
     if(r.Length>0)r+="&";
     r+=d.Field+"="+d.Value;
     }
     return r;
     }
     
     public void AddField(string Field,string Value)
      {
     data a=new data();
     a.Field=Field;
     a.Value=Value;
     
     arr.Add(a);
     }
     
     struct data
      {
     public string Field,Value;
     }
     
     
     }可以参考