HttpWebRequest 哪种开发语言网站无法模拟登录 本帖最后由 cn1098035521 于 2011-03-19 15:29:59 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 myRequest.ContentType = "text/html";改为 application/x-www-form-urlencoded 换成后,提示下面的消息-------------------------------------------------- <br /> 非常抱歉!</B></BIG></P> <div id="ctl00_ctl00_cph_cph_mymsg"> <P> 由于您所请求的页面(/User/Login.aspx)存在异常,因此程序无法正确处理您的请求。</P> <p> 造成该异常的发生,可能由于以下情况(但不限于):<br /> </p></div> <span id="ctl00_ctl00_cph_cph_lblErrorMessage"><font color="#FF8080"><li>如果您刚刚是在填写表单,请确认所提交的表单中不含有特别内容,这些内容包括HTML代码等。</li> <li>我们的工程师正在对网站进行维护。</li></font></span><br /> 有可能,我遇到两次了,input name="login$username" 这种带有$符号的都出现这种情况aspx的页面 http://wo.zp365.com/public/loginZPGY.aspx这个网站的登录页面代码也是一样,采用同一种方式,也是登录不了 你用 HttpUtility.UrlEncode 编码下:/ <summary> /// RequestUrl: request some url, return the response. /// </summary> /// <param name="strUrl">the url</param> /// <param name="strPostDatas">the post data</param> /// <param name="objCookieContainer">cookie's(session's) container</param> /// <returns></returns> public static string RequestUrl(string strUrl, string strPostDatas, ref CookieContainer objCookieContainer) { HttpWebResponse res = null; string strResponse = ""; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl); req.Method = "POST"; req.KeepAlive = true; req.ContentType = "application/x-www-form-urlencoded"; //req.Timeout = 20; if(objCookieContainer == null) objCookieContainer = new CookieContainer(); req.CookieContainer = objCookieContainer; StringBuilder objEncodedPostDatas = new StringBuilder(); byte[] postDatas = null; req.ContentLength = 0; if (strPostDatas != null && strPostDatas.Length > 0) { string[] datas = strPostDatas.TrimStart('?').Split(new char[] {'&'}); for(int i=0; i<datas.Length; i++) { string[] keyValue = datas[i].Split(new char[] {'='}); if(keyValue.Length >= 2) { objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[0])); objEncodedPostDatas.Append("="); objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[1])); if(i < datas.Length - 1) { objEncodedPostDatas.Append("&"); } } } postDatas = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString()); req.ContentLength = postDatas.Length; using(Stream reqStream = req.GetRequestStream()) { reqStream.Write(postDatas, 0, postDatas.Length); } } res = (HttpWebResponse)req.GetResponse(); objCookieContainer = req.CookieContainer; using(Stream resStream = res.GetResponseStream()) { using(StreamReader sr = new StreamReader(resStream, Encoding.UTF8)) { strResponse = sr.ReadToEnd(); } } } //catch (Exception ex) //{ // strResponse = ex.ToString(); //} finally { if (res != null) { res.Close(); } } return strResponse; } 成功了fangxinggood,太感谢你了!! .Net GridView 如何在服务端设置选中某一行 openfire 和agsxmpp 通过程序让电脑的声音放大N倍 关于C#编写程序求解! 怎么用C#复制SQL Server中的某表并重命名 local IP 关于axWebBrowser,比较奇怪的一个问题,各位高手看一下拉 大家快来看看这算不算URL重写的一个BUG c#中为什么加入控件就不能运行 多多指点 求救 紧急 很简单 如何用泛型在EventHandler委托中声明自定义事件数据? vs2010调用皮肤类库IrisSkin2.dll
改为
application/x-www-form-urlencoded
换成后,提示下面的消息
--------------------------------------------------
<br />
非常抱歉!</B></BIG></P>
<div id="ctl00_ctl00_cph_cph_mymsg">
<P>
由于您所请求的页面(/User/Login.aspx)存在异常,因此程序无法正确处理您的请求。</P>
<p>
造成该异常的发生,可能由于以下情况(但不限于):<br />
</p></div>
<span id="ctl00_ctl00_cph_cph_lblErrorMessage"><font color="#FF8080"><li>如果您刚刚是在填写表单,请确认所提交的表单中不含有特别内容,这些内容包括HTML代码等。</li>
<li>我们的工程师正在对网站进行维护。</li></font></span><br />
/// RequestUrl: request some url, return the response.
/// </summary>
/// <param name="strUrl">the url</param>
/// <param name="strPostDatas">the post data</param>
/// <param name="objCookieContainer">cookie's(session's) container</param>
/// <returns></returns>
public static string RequestUrl(string strUrl, string strPostDatas, ref CookieContainer objCookieContainer)
{
HttpWebResponse res = null;
string strResponse = ""; try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
//req.Timeout = 20; if(objCookieContainer == null)
objCookieContainer = new CookieContainer();
req.CookieContainer = objCookieContainer;
StringBuilder objEncodedPostDatas = new StringBuilder();
byte[] postDatas = null; req.ContentLength = 0;
if (strPostDatas != null && strPostDatas.Length > 0)
{
string[] datas = strPostDatas.TrimStart('?').Split(new char[] {'&'});
for(int i=0; i<datas.Length; i++)
{
string[] keyValue = datas[i].Split(new char[] {'='});
if(keyValue.Length >= 2)
{
objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[0]));
objEncodedPostDatas.Append("=");
objEncodedPostDatas.Append(HttpUtility.UrlEncode(keyValue[1]));
if(i < datas.Length - 1)
{
objEncodedPostDatas.Append("&");
}
}
}
postDatas = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString());
req.ContentLength = postDatas.Length;
using(Stream reqStream = req.GetRequestStream())
{
reqStream.Write(postDatas, 0, postDatas.Length);
}
} res = (HttpWebResponse)req.GetResponse();
objCookieContainer = req.CookieContainer;
using(Stream resStream = res.GetResponseStream())
{
using(StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
{
strResponse = sr.ReadToEnd();
}
}
}
//catch (Exception ex)
//{
// strResponse = ex.ToString();
//}
finally
{
if (res != null)
{
res.Close();
}
}
return strResponse;
}