现在碰到了一个问题,我先把代码贴出来! try
{
string url = "http://domainname/Login.do"; string urlExt = "username=passenger&password=qqqqqq";
WebRequest req = WebRequest.Create(url); //置属性
req.Method = "POST"; //开始请求
byte[] buff = Encoding.GetEncoding("UTF-8").GetBytes(urlExt);
req.ContentLength = buff.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
reqStream.Flush();
reqStream.Close(); //接收返回字串
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
this.rtbCont.Text = sr.ReadToEnd(); }
catch (WebException ex)
{
ex.GetMessage();
}首先声明,这段代码是完全可以正常访问到远程的!但是现在在局域网里,向外访问的时候,却出现了异常(异常信息是超时);而使用IE做同样的操作却完全正常——就是正常的敲入地址、显示、提交!搞不明白!按我的理解,可能是网络防火墙的设置有问题,但是什么问题呢?抑或是.NET的缺憾——这个可能性不大,但是是什么呢,请高人帮忙解答下!在线等!!!谢谢!
{
string url = "http://domainname/Login.do"; string urlExt = "username=passenger&password=qqqqqq";
WebRequest req = WebRequest.Create(url); //置属性
req.Method = "POST"; //开始请求
byte[] buff = Encoding.GetEncoding("UTF-8").GetBytes(urlExt);
req.ContentLength = buff.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
reqStream.Flush();
reqStream.Close(); //接收返回字串
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
this.rtbCont.Text = sr.ReadToEnd(); }
catch (WebException ex)
{
ex.GetMessage();
}首先声明,这段代码是完全可以正常访问到远程的!但是现在在局域网里,向外访问的时候,却出现了异常(异常信息是超时);而使用IE做同样的操作却完全正常——就是正常的敲入地址、显示、提交!搞不明白!按我的理解,可能是网络防火墙的设置有问题,但是什么问题呢?抑或是.NET的缺憾——这个可能性不大,但是是什么呢,请高人帮忙解答下!在线等!!!谢谢!
解决方案 »
- for i=1 to rs.pagesize 在c#下 等同于哪个语句?
- 查询数据库一直提示错误,实在看不出来了。望大家不吝赐教,多谢!!!
- webservice 的问题
- .net网络编程前景如何?用什么书籍比较好?
- DropDownList的SelectedIndexChanged事件虽然触发了,但为什么触发后没有选中正确的项?
- ftp出问题了,不知道这个是什么原因呀???
- 讨论下O/R Maping工具
- AspNetForums2.0装不上,高手帮忙!
- 我想设计一个在局域网上实现通信的程序,有些疑或想请教网络编程高手
- 我使用.net的连接向导连接sql数据库,但不能修改,只能增加,为什么?
- C# 发展趋势
- oracle存储过程的问题
完善设置你的request的属性比如:ContentType,Accept,UserAgent,KeepAlive试试
使用IE时的头:
POST /Login.do HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, 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, */*
Referer: http://domainname/
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: domainname
Content-Length: 122
Proxy-Connection: Keep-Alive
Pragma: no-cache
Cookie: [email protected]; syshomeforreg=1; __utma=14713004.1585016267.1244094161.1244522669.1244525972.17; __utmz=14713004.1244525972.17.15.utmccn=(referral)|utmcsr=kaixin.com|utmcct=/|utmcmd=referral; depovince=TJ; ck_for_foot_inc=ck_for_foot_inc_value; rgsu=80305; _reg_stage=reg_complete; _user_stage=null; __utmb=14713004; XNESSESSIONID=abcrIWx9LCWfvbniGFdhs; userid=704383999; univid=0; gender=0; univyear=0; hostid=704383999; xn_app_histo_704383999=2-3-6-1001294-45-1000019-24997-20805-23917-24985; __utmc=14713004; xns=12622206465300email=passenger&password=qqq
使用程序时的头:
POST /Login.do HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Referer: http://domainname/
Content-Type: application/x-www-form-urlencoded
Host: domainname
Content-Length: 122
Expect: 100-continue
Proxy-Connection: Keep-Aliveemail=passenger&password=qqq
大家看看,程序里还需要改什么?
req.ServicePoint.Expect100Continue = false;就搞定了。 try
{
string url = "http://domainname/Login.do"; string urlExt = "username=passenger&password=qqqqqq";
WebRequest req = WebRequest.Create(url); //置属性
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
req.Referer = "http://domainname/";
req.CookieContainer = new CookieContainer();
req.ContentType = "application/x-www-form-urlencoded";
req.ServicePoint.Expect100Continue = false;
byte[] buff = Encoding.GetEncoding("UTF-8").GetBytes(urlExt);
req.ContentLength = buff.Length;
req.Method = "POST"; //开始请求
byte[] buff = Encoding.GetEncoding("UTF-8").GetBytes(urlExt);
req.ContentLength = buff.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
reqStream.Flush();
reqStream.Close(); //接收返回字串
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
this.rtbCont.Text = sr.ReadToEnd(); }
catch (WebException ex)
{
ex.GetMessage();
}