public class TD3HttpPro
{
private string _url = string.Empty;
private string _postdate = string.Empty;
private string _refUrl = string.Empty;
private System.Text.Encoding _encoding = System.Text.Encoding.Default;
private CookieCollection _cookiesPost = null;
private CookieCollection _cookiesGet = null;
private string _strCode;
private string _ResHtml;
private string _strErr;
public string CreateRequest()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this._url);//创建req
req.Accept = "*/*"; //接受任意文件
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览
req.KeepAlive = true;
if (_refUrl != null & _refUrl.Length > 0)
{
req.Referer = _refUrl;
} //以Post方式发送数据
if (_postdate != null & _postdate.Length > 0)
{ req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] b = Encoding.Default.GetBytes(_postdate);
System.IO.Stream sw = null;
try
{
sw = req.GetRequestStream();
sw.Write(b, 0, b.Length);
}
catch (System.Exception ex)
{
this._strErr = ex.Message;
}
finally
{
if (sw != null) { sw.Close(); }
}
}
if (req.CookieContainer == null)
{
req.CookieContainer = new CookieContainer();
}
//获取Cook
if (this._cookiesPost != null)
{
System.Uri u = new Uri(this._url);
//doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取! //这里修改成一致!
foreach (System.Net.Cookie c in this._cookiesPost)
{
c.Domain = u.Host;
}
req.CookieContainer.Add(this._cookiesPost);
}
HttpWebResponse rep = null;
System.IO.StreamReader sr = null;
try
{
rep = (HttpWebResponse)req.GetResponse();
sr = new System.IO.StreamReader(rep.GetResponseStream(), this._encoding);
this._cookiesPost = rep.Cookies;
}
catch { }
return sr.ReadToEnd();
}
public TD3HttpPro(string url, string PostDate)
{
this.Url = url;
this.PostDate = PostDate;
}
public TD3HttpPro(string url)
{
this.Url = url;
}
public TD3HttpPro(string url, string PostDate, System.Net.CookieCollection Cookies)
{
this.Url = url;
this.PostDate = PostDate;
}
#endregion #region 属性
public System.Text.Encoding encoding
{
get { return _encoding; }
set { _encoding = value; }
}
public string StrCode
{
get { return _strCode; }
set { _strCode = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public string PostDate
{
get { return _postdate; }
set { _postdate = value; }
}
public CookieCollection CookiesPost
{
get { return _cookiesPost; }
set { _cookiesPost = value; } }
public CookieCollection CookiesGet
{
get { return _cookiesGet; }
}
public string RefUrl
{
get { return _refUrl; }
set { _refUrl = value; }
}
public string StrErr
{
get { return _strErr; }
set { _strErr = value; }
}
public string ResHtml
{
get
{ return _ResHtml; }
}
}为什么我老是不能登陆啊
麻烦高手指点啊
我这个是从CSdN助手里边Copy过来的,怎么也不能登陆啊抓取的网页
GET /index.jsp HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.1.1:9080HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.1.GA (build: SVNTag=JBoss_4_2_1_GA date=200707131605)/Tomcat-5.5
Set-Cookie: JSESSIONID=A042450B10795CFCFB060C97987CBCDD; Path=/
Content-Type: text/html;charset=GB2312
Transfer-Encoding: chunked
Date: Wed, 18 Jun 2008 15:10:51 GMT
{
private string _url = string.Empty;
private string _postdate = string.Empty;
private string _refUrl = string.Empty;
private System.Text.Encoding _encoding = System.Text.Encoding.Default;
private CookieCollection _cookiesPost = null;
private CookieCollection _cookiesGet = null;
private string _strCode;
private string _ResHtml;
private string _strErr;
public string CreateRequest()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this._url);//创建req
req.Accept = "*/*"; //接受任意文件
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览
req.KeepAlive = true;
if (_refUrl != null & _refUrl.Length > 0)
{
req.Referer = _refUrl;
} //以Post方式发送数据
if (_postdate != null & _postdate.Length > 0)
{ req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] b = Encoding.Default.GetBytes(_postdate);
System.IO.Stream sw = null;
try
{
sw = req.GetRequestStream();
sw.Write(b, 0, b.Length);
}
catch (System.Exception ex)
{
this._strErr = ex.Message;
}
finally
{
if (sw != null) { sw.Close(); }
}
}
if (req.CookieContainer == null)
{
req.CookieContainer = new CookieContainer();
}
//获取Cook
if (this._cookiesPost != null)
{
System.Uri u = new Uri(this._url);
//doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取! //这里修改成一致!
foreach (System.Net.Cookie c in this._cookiesPost)
{
c.Domain = u.Host;
}
req.CookieContainer.Add(this._cookiesPost);
}
HttpWebResponse rep = null;
System.IO.StreamReader sr = null;
try
{
rep = (HttpWebResponse)req.GetResponse();
sr = new System.IO.StreamReader(rep.GetResponseStream(), this._encoding);
this._cookiesPost = rep.Cookies;
}
catch { }
return sr.ReadToEnd();
}
public TD3HttpPro(string url, string PostDate)
{
this.Url = url;
this.PostDate = PostDate;
}
public TD3HttpPro(string url)
{
this.Url = url;
}
public TD3HttpPro(string url, string PostDate, System.Net.CookieCollection Cookies)
{
this.Url = url;
this.PostDate = PostDate;
}
#endregion #region 属性
public System.Text.Encoding encoding
{
get { return _encoding; }
set { _encoding = value; }
}
public string StrCode
{
get { return _strCode; }
set { _strCode = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public string PostDate
{
get { return _postdate; }
set { _postdate = value; }
}
public CookieCollection CookiesPost
{
get { return _cookiesPost; }
set { _cookiesPost = value; } }
public CookieCollection CookiesGet
{
get { return _cookiesGet; }
}
public string RefUrl
{
get { return _refUrl; }
set { _refUrl = value; }
}
public string StrErr
{
get { return _strErr; }
set { _strErr = value; }
}
public string ResHtml
{
get
{ return _ResHtml; }
}
}为什么我老是不能登陆啊
麻烦高手指点啊
我这个是从CSdN助手里边Copy过来的,怎么也不能登陆啊抓取的网页
GET /index.jsp HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.1.1:9080HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.1.GA (build: SVNTag=JBoss_4_2_1_GA date=200707131605)/Tomcat-5.5
Set-Cookie: JSESSIONID=A042450B10795CFCFB060C97987CBCDD; Path=/
Content-Type: text/html;charset=GB2312
Transfer-Encoding: chunked
Date: Wed, 18 Jun 2008 15:10:51 GMT
1、你说的“doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取!”
这是http协议要求的,不是.net的问题,每个cookies本身就是与一定的地域联系在一起的。为了保护你的隐私,
不同的Host中的cookies绝对不允许发送。
2、登陆网站需要验证码:很明显,一般的验证码请求时会给你发送一串cookies,你必须把得到验证码的那次请求返回的cookie,在post时一并提交出去。当然,关于验证码的识别,你需要另外的处理,但是cookie一定要保存。(如果多次请求验证码页面,还有需要更新cookies)
3、关于req.ContentType = "application/x-www-form-urlencoded";
而你发送时却是Encoding.Default.GetBytes(string)
你的这个没有使用application/x-www-form-urlencoded编码,如果都是英文(不含特殊符号)时,可能有幸通过,但是,有了中文和
特殊符号,服务器肯定解释出错。
你把这几点处理完毕,基本上就能搞定了。