WebRequest + 에이전트 + POST 로그 인

11771 단어 request
WebRequest + 에이전트 + POST 로그 인소프트웨어 아카데미과학 기술 시대시 나 닷 컴
WebRequest + 에이전트 + POST 로그 인
http://www.sina.com.cn 2008 년 07 월 08 일 13:07 
IT168.com
[IT 168 기술 문서]

   
     public static void ProxySetting(WebRequest request)
{
WebProxy proxy = WebProxy.GetDefaultProxy();//  IE    
//        ,               ,                  Proxy
              if (proxy.Address == null)
proxy.Address = new Uri("××××××:8080");//       Proxy   
              if (proxy.Address != null)//
              {
proxy.Credentials = new NetworkCredential("test123","123456");//          
                  request.Proxy = proxy;//   request.Proxy 
              }
}
public static string GetHtmlOne(string url)
{
url = url.Replace("&", "&");
string indata = "userid=name&passwd=password&SESSION_TIMEOUT=40";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//    CookieContainer   Cookie   
              HttpWebRequest myHttpWebRequest;
myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http:// www.12355.com/logincheck.aspx");
//    HttpWebRequest 
              myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
ProxySetting(myHttpWebRequest);
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();
//  "  "
//   Cookie,                    
              myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
ProxySetting(myHttpWebRequest);//
              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();
return outdata;
//    "  "
//   *    ,   "    " 

}

좋은 웹페이지 즐겨찾기