日期:2014-05-18  浏览次数:20772 次

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