日期:2014-05-20  浏览次数:20951 次

关于WebClient的DownloadData奇怪问题,有知道的大大麻烦进来看哈!
事情是这样的:
我写了一个.net的程序,具体代码如下:
C# code
 WebClient mywebclient = new WebClient();
        byte[] bytes = mywebclient.DownloadData("http://seasoncoffee.cn.alibaba.com/athena/contact/seasoncoffee.html");

 
运行后出现错误:
远程服务器返回错误: (404) 未找到。 
但是我将“http://seasoncoffee.cn.alibaba.com/athena/contact/seasoncoffee.html”复制到IE中又可以直接访问,说明该地址是存在的。
后来我又将“http://seasoncoffee.cn.alibaba.com/athena/contact/seasoncoffee.html”这个地址换成了“http://www.baidu.com”,程序成功运行。

难道是“http://seasoncoffee.cn.alibaba.com/athena/contact/seasoncoffee.html”禁止什么访问权限吗?想了半天,实在不解。。。

有哪位高手知道的麻烦说下,谢谢了~~




------解决方案--------------------
帮顶一下.
------解决方案--------------------
找个http调试工具看看ie给钱一个网站发送了什么消息头。
------解决方案--------------------
--------------only to be reference 
我是用的mshtml,的axWebBrowser控件,如果你有兴趣的话 给你复制代码。
------解决方案--------------------
给你一个 我写的方法吧 没用控件


C# code

 /// <summary>
    /// 访问网络上的文件HTML
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static byte[] webRequestByte(string url) {

        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream resStream = response.GetResponseStream();  
        int bytelength=(int)response.ContentLength;
        Byte[] bytevalue = new Byte[bytelength];
       // resStream.Read(bytevalue, 0, bytevalue.Length);  
        int num=0;
        int m;
        while (1 > 0)
        {
            m = resStream.Read(bytevalue, num, bytelength);
            if(m==0)break;
            num += m;           //当前已下载的字节数
            bytelength -= m;    //剩余字节数   总字节数:(int)response.ContentLength                            
        }
        resStream.Close();

        return bytevalue;

       // StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);  //转换成字符串 输出
      //  ContentHtml.Text = sr.ReadToEnd();
        // sr.Close();

        //WebClient 得到字节流
        //WebClient wc=new WebClient();
        // Stream srm=wc.OpenRead(TempDown);
        
        //区别在于  WebRequest 是 system.net 空间下的 抽象类,你可以对它进行扩展
        //WebClient 非抽象类

    }