现在碰到了一个问题,我先把代码贴出来!            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的缺憾——这个可能性不大,但是是什么呢,请高人帮忙解答下!在线等!!!谢谢!

解决方案 »

  1.   

    直接使用HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
      

  2.   

    用Fiddler看看两者出去的请求是不是一模一样,我感觉可能有的头浏览器有,软件里面有,防火墙一类的东西就把他作为病毒发包给丢了
      

  3.   

    首先感谢2楼的大哥,改完还是不行(WebRequest--〉HttpWebRequest、WebResponse--〉HttpWebResponse)。再回复3楼,不需要代理!
      

  4.   

    单步一下 ,看是在哪一步的时候超时
    完善设置你的request的属性比如:ContentType,Accept,UserAgent,KeepAlive试试
      

  5.   

    嗯,我再试试!具体出错的地方是在:    WebResponse res = req.GetResponse();这一句返回超时错。
      

  6.   

    经过追踪之后的http头如下:
    使用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
    大家看看,程序里还需要改什么?
      

  7.   

    搞定,根据http头里的Expect: 100-continue,加了如下一句:
       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(); 
                }