HttpWebRequest提交对方网站,有的页面成功,有的不成功。几天了,一直没搞定,请高手帮忙解决,非常感谢!
因项目需要,需要采用HttpWebRequest将相关内容自动提交到对方系统
下面是我的提交方式
采用这种方式大部分页面是可以正常提交的,但是有几个页面总是会出错(其实也就是跳转到了他们的一个错误页),我获取的不是正常情况下应该获取的数据,而是他们的错误页的数据。
出错的几个页面,IE6是可以正常访问的
下面是代码:
Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
string sAction = "http://10.223.111.45/pms/exchange/save.so";
string sData = "readMark=null&eqType=01&appNo=101003941794&equipCateg=01&equipId=2010000001460677&assetNo=43410010000005268&chkGroup=&chkGroupName=&checkerNo=&checkerNoName=&chkDate=&chkConcCode=1&chkContent=";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sAction);
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
req.Headers["Accept-Encoding"] = "gzip, deflate";
req.Headers["Accept-Language"] = "zh-cn";
req.Headers["Cache-Control"] = "no-cache";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
req.Referer = "http://10.223.111.45/pms/exchange/save.so";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(this.LoginCookie);//把前面登陆后获取的COOKIE加进来
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(sData);
req.ContentLength = PostData.Length;
Stream tempStream = req.GetRequestStream();
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), enc);
string sHtml = sr.ReadToEnd();
sr.Close();
res.Close();
下面是我的提交方式
采用这种方式大部分页面是可以正常提交的,但是有几个页面总是会出错(其实也就是跳转到了他们的一个错误页),我获取的不是正常情况下应该获取的数据,而是他们的错误页的数据。
出错的几个页面,IE6是可以正常访问的
下面是代码:
Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
string sAction = "http://10.223.111.45/pms/exchange/save.so";
string sData = "readMark=null&eqType=01&appNo=101003941794&equipCateg=01&equipId=2010000001460677&assetNo=43410010000005268&chkGroup=&chkGroupName=&checkerNo=&checkerNoName=&chkDate=&chkConcCode=1&chkContent=";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sAction);
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
req.Headers["Accept-Encoding"] = "gzip, deflate";
req.Headers["Accept-Language"] = "zh-cn";
req.Headers["Cache-Control"] = "no-cache";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
req.Referer = "http://10.223.111.45/pms/exchange/save.so";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(this.LoginCookie);//把前面登陆后获取的COOKIE加进来
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(sData);
req.ContentLength = PostData.Length;
Stream tempStream = req.GetRequestStream();
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), enc);
string sHtml = sr.ReadToEnd();
sr.Close();
res.Close();
...全文
相关推荐
C#通过一个页面按钮调用另一个页面的按钮(不打开页面的前提下)Response,HttpWebRequest方法
本篇将详细讲解如何在不打开新页面的情况下,通过C#代码利用Response和HttpWebRequest方法实现一个页面按钮调用另一个页面的按钮的功能。
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
### C#实现通过HttpWebRequest发送POST请求实现网站自动登陆#### 概述在Web开发领域,有时我们需要实现一些自动化操作,比如模拟用户登录过程、提交表单数据等。
使用HttpWebRequest提交数据
### 使用HttpWebRequest提交数据#### 一、概述在.NET框架中,`HttpWebRequest`是`System.Net`命名空间下的一个重要类,它提供了一种简便的方式来进行HTTP
httpwebrequest抓取页面小工具
**HttpSend工具:**"HttpSend"可能是提供这些功能的一个小工具,它可能封装了HttpWebRequest的用法,方便用户快速发送GET和POST请求,抓取页面数据。
C#(Csharp)多线程HTTP并发请求(HttpWebRequest)采集蜘蛛
通过良好的设计和优化,可以构建出能够处理大量并发请求的采集程序,同时确保对目标网站的影响最小化。在实际应用中,还需要考虑爬虫的道德规范,尊重网站的Robots协议,避免对服务器造成过大的负担。


