一个WebRequest+代理+POST登录

一个WebRequest+代理+POST登录_软件学园_科技时代_新浪网

一个WebRequest+代理+POST登录

http://www.sina.com.cn 2008年07月08日 13:07  IT168.com

【IT168技术文档】

   
     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;
//再次显示"登录"
//如果把*行注释调,就显示"没有登录" 

}

你可能感兴趣的:(request)