我自己写的,用WebClient访问,为什么不能访问下面的链接呢?提示不能连接远程服务器
static void Main(string[] args)
{
            WebClient MyWebClient = new WebClient();
            Byte[] html = MyWebClient.DownloadData("https://detail.1688.com/offer/44007847996.html?spm=0.0.0.0.1P0sGD");    //获取html
            string  pageHtml = Encoding.Default.GetString(html);
          //此处插入正则匹配,获取价格……略……还没学会
           Console.WriteLine(pageHtml);    //显示下载的html
           Console.Read();
}
我目的是想从阿里巴巴某件商品网页自动获取商品价格,跪求前辈指点……

解决方案 »

  1.   

    字符串前面加@,Byte[] html = MyWebClient.DownloadData(@"https://xxxxx");
      

  2.   

    还是不行,提示不能连接远程服务器
    Byte[] pageData = MyWebClient.DownloadData(@"https://detail.1688.com/offer/44007847996.html?spm=0.0.0.0.1P0sGD");
      

  3.   

            static void Main(string[] args)
            {            WebClient client = new WebClient();            Byte[]  sb= client.DownloadData("http://www.cnblogs.com");
                string res = Encoding.UTF8.GetString(sb);
                Console.WriteLine(res);
                Console.ReadLine();            Byte[] sb1 = client.DownloadData(@"https://detail.1688.com/offer/44007847996.html?spm=0.0.0.0.1P0sGD");    //获取html
                res = Encoding.UTF8.GetString(sb1);
                Console.WriteLine(res);
                Console.ReadLine();
            }前面是正常的,后面是不对,可能是因为https的原因?
    提示:
    未处理 System.Net.WebException
      HResult=-2146233079
      Message=无法连接到远程服务器
      Source=System
      StackTrace:
           在 System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
           在 System.Net.WebClient.DownloadData(Uri address)
           在 System.Net.WebClient.DownloadData(String address)
           在 ConsoleApp1.ClassWebclient.Main(String[] args)
           在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.Net.Sockets.SocketException
           HResult=-2147467259
           Message=由于目标计算机积极拒绝,无法连接。 127.0.0.1:80
           Source=System
           ErrorCode=10061
           NativeErrorCode=10061
           StackTrace:
                在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
                在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
           InnerException: 
    另外,获取dom结点,建议使用htmlpaster或者xmlDocument工具和xpath进行导航,比正则要好。
      

  4.   

    WebClient 不支持 https。你可以使用 HttpWebRequest 方式访问,并且要注意为其对象的 ClientCertificates 集合加入你的证书,才能访问服务器。 
      

  5.   

    百度搜下 周金桥的帖子,C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求
    https://www.cnblogs.com/youfeng365/p/6138944.html