为什么我无法使用webclient访问Yobit API

时间:2018-02-07 10:39:18

标签: webclient

以前的代码。

相关网址为

https://yobit.net/api/3/info

它适用于IE。它曾经与webclient一起使用。它现在在webclient中不起作用。我想知道问题是什么

突然停止工作。所以我正在检查

    Try
        Dim wc = New WebClient
        wc.Headers.Add("Accept", "text/html, application/xhtml+xml, image/jxr, */*")
        wc.Headers.Add("Accept-Encoding", "gzip, deflate")
        wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko")
        wc.Headers.Add("Accept-Language", "en-US,en;q=0.5")
        wc.DownloadString(URL)
    Catch ex As Exception

    End Try

我也试过简单的版本。不工作

    Try
        Dim wc = New WebClient
        wc.DownloadString(URL)
    Catch ex As Exception

    End Try

在这两种情况下,yobit抛出 503访问被拒绝异常

我使用fiddler并尝试使用internetexplorer直接访问

工作正常

GET https://yobit.net/api/3/info HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: en-US,en;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: yobit.net
Connection: Keep-Alive
Cookie: __cfduid=de63c60d603f271520b9ee58dfdd257061517932785; cf_clearance=7e58588df28b267842f753567dcdc475d29679a6-1517932789-86400; locale=en

如果我使用webclient,这就是标题

GET https://yobit.net/api/3/info HTTP/1.1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Accept: text/html, application/xhtml+xml, image/jxr, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: yobit.net
Connection: Keep-Alive

几乎完全相同的事情。

让我尝试另一个网址

http://google.com

GET http://www.google.com/ HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: en-US,en;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: www.google.com
Connection: Keep-Alive
Cookie: NID=121=GUd4VKHT_gcwUx-hK39mphuCg93Q_W2fL_yCc-JO3AJkgh74EGajif0537eraLK8ns2EdEQPexOOeBxSlOxVrj8t_AVn21FRme2hAxuLXz4F8aCZExIzME4jaYMBuUp_lnak5Q; OGPC=19004116-3:; 1P_JAR=2018-1-9-7

如果我使用webclient

GET http://google.com/ HTTP/1.1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Accept: text/html, application/xhtml+xml, image/jxr, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: google.com
Connection: Keep-Alive

两者都有效。

1 个答案:

答案 0 :(得分:2)

这里可能存在不同的问题

框架版

您最近更改了框架版本,并且有一些问题导致它无法正常工作。您可以更改框架版本并查看是否有帮助

压缩数据

默认情况下,

WebClient不进行解压缩,如果可用,您可以添加标头以请求gzip数据。现在有可能该网站早先没有启用gzip,这使得它可以为你工作,现在他们已经启用了gzip响应。您可以通过更改

来解决这个问题
wc.Headers.Add("Accept-Encoding", "gzip, deflate")

wc.Headers.Add("Accept-Encoding", "deflate")

或者如果您希望数据仅以压缩形式出现,那么您可以启用自动解压缩,如下面的代码所示

class Program
{
    class MyWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address) as HttpWebRequest;
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            return request;
        }
    }
    public static void Main(string[] args)
    {
        var URL = "https://yobit.net/api/3/info";
        var wc = new WebClient();
        wc.Headers.Add("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
        wc.Headers.Add("Accept-Encoding", "gzip, deflate");
        wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
        wc.Headers.Add("Accept-Language", "en-US,en;q=0.5");
        Console.WriteLine(wc.DownloadString(URL));
        // TODO: Implement Functionality Here

        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}

以上示例为C#而不是VB.NET,但您可以轻松转换它

Working