首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

HttpWebRequest 又来了…解决方法

2012-06-21 
HttpWebRequest 又来了……C# codeHttpWebRequest httpWebRequest nullHttpWebResponse httpWebResponse

HttpWebRequest 又来了……

C# code
HttpWebRequest httpWebRequest = null;            HttpWebResponse httpWebResponse = null;                    var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");                    var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");                    var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");            try            {                byte[] byteRequest = Encoding.Default.GetBytes(postData);                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.binzhou.cc/ershou_Rental.asp?act=save");                httpWebRequest.CookieContainer = cookieContainer;                httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;                httpWebRequest.ServicePoint.ConnectionLimit = maxTry;                httpWebRequest.Referer = "http://www.binzhou.cc/ershou_Rental.asp";                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */* "                           httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";                httpWebRequest.Method = "POST";                httpWebRequest.ContentLength = byteRequest.Length;                httpWebRequest.AllowAutoRedirect = true;                Stream stream = httpWebRequest.GetRequestStream();                stream.Write(byteRequest, 0, byteRequest.Length);                stream.Close();                try                {                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();                }                catch (WebException ex)                {                    httpWebResponse = (HttpWebResponse)ex.Response;                }                //httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();                httpWebResponse.Cookies = cookieContainer.GetCookies(httpWebRequest.RequestUri);                Stream responseStream = httpWebResponse.GetResponseStream();                StreamReader streamReader = new StreamReader(responseStream, encoding);                //cc.SetCookies(httpWebRequest.RequestUri);                cc = cookieContainer;                if (!(PublishVoid.htLogin.Contains(url)))                    PublishVoid.htLogin.Add(url, cc);                string html = streamReader.ReadToEnd();                streamReader.Close();                responseStream.Close();                currentTry = 0;                httpWebRequest.Abort();                httpWebResponse.Close();                return html;            }

运行返回的html为:Microsoft VBScript 运行时错误,无效的过程调用或参数: 'leftb'

http://www.binzhou.cc/ershou_Rental.asp?act=save页面访问时500错误
但是
http://www.binzhou.cc/ershou_Rental.asp的form为
<form action="?act=save" method="post" enctype="multipart/form-data" name="form2" id="form2" onsubmit="return checkData(this)">
帮忙,谢谢!!!

[解决办法]
postData时不时不符合网站录入的要求,仔细看下
[解决办法]
好像是 post 过去的数据无效,仔细检查一下。另外看看 Encoding 和对方网站一致不
[解决办法]
楼主把postdata完整数据发来
[解决办法]
可能是因为这种格式不正确导致的,你试试模拟上面这种模式post


[解决办法]
500 多检查HTTP报头信息
[解决办法]
LZ想干坏事。

其它这样做是没啥意义的。伪造的请求

asp.net MVC有个新东东 Anti forgery token ,防止伪造请求。

你先用fiddler构建个请求试试,再自己写程序。
[解决办法]
自己写SOCKET发送,不用HttpWebRequest
[解决办法]
<form action="?act=save" method="post" enctype="multipart/form-data" name="form2" id="form2" onsubmit="return checkData(this)">
提交之前还执行了这个事件,看看这里面干什么了。
[解决办法]
StringBuilder post = new StringBuilder();
string boundary = DateTime.Now.Ticks.ToString("x");
string data = "Region=滨城&Units=二室一厅&HouseType=高层住宅&Price=3500&BuildingArea=60&HouseAddress=黄河二路渤海十八路路南&ContactMan=刘先生&ContactTel=&ContactMobile=13891937053&PropertyRight=个人产权

&HouseStructure=钢混结构&TotalFloor=8&Floor=18&RepairDegree=豪华装修&ConstructionTime=2008&PaymentMethod=面议&Validity=一周&Infrastructure=水&Infrastructure=电&OtherExplanations=位于黄河二路渤海十八路现代城

小区,80平方,两室一厅一卫,有沙发,茶几,电视柜,暖气,双人床,太阳能,打火灶&Photo=&Submit22=发布";
string[] array = data.Split('&');
foreach (string str in array)
{
string[] arr = str.Split('=');
post.AppendFormat("-----------------------------{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
}
post.AppendFormat("-----------------------------{0}--\r\n", boundary);
//这里取post.ToString()获取请求数据,也就是你的postData。
//最后还有一点,要统一编码,看网站是什么编码的。*************************************************
本内容使用CSDN 小秘书回复
每天回帖即可得10分可用分!
*************************************************
[解决办法]

C# code
StringBuilder post = new StringBuilder();string boundary = DateTime.Now.Ticks.ToString("x");string data = "Region=滨城&Units=二室一厅&HouseType=高层住宅&Price=3500&BuildingArea=60&HouseAddress=黄河二路渤海十八路路南&ContactMan=刘先生&ContactTel=&ContactMobile=13891937053&PropertyRight=个人产权&HouseStructure=钢混结构&TotalFloor=8&Floor=18&RepairDegree=豪华装修&ConstructionTime=2008&PaymentMethod=面议&Validity=一周&Infrastructure=水&Infrastructure=电&OtherExplanations=位于黄河二路渤海十八路现代城小区,80平方,两室一厅一卫,有沙发,茶几,电视柜,暖气,双人床,太阳能,打火灶&Photo=&Submit22=发布";string[] array = data.Split('&');foreach (string str in array){    string[] arr = str.Split('=');    post.AppendFormat("-----------------------------{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);}post.AppendFormat("-----------------------------{0}--\r\n", boundary);//这里取post.ToString()获取请求数据,也就是你的postData。//最后还有一点,要统一编码,看网站是什么编码的。 

热点排行