简体   繁体   English

获取System.Net.WebException:远程服务器返回错误:(403)Forbidden。在Google URL Shortener API Call中

[英]Getting System.Net.WebException: The remote server returned an error: (403) Forbidden. in Google URL Shortener API Call

I am using below code to shorten long urls 我使用下面的代码来缩短长网址

public static string UrlShorten(string url)
{
    string post = "{\"longUrl\": \"" + url + "\"}";
    string shortUrl = url;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + ReadConfig("GoogleUrlShortnerApiKey"));
    try
    {
        request.ServicePoint.Expect100Continue = false;
        request.Method = "POST";
        request.ContentLength = post.Length;
        request.ContentType = "application/json";
        request.Headers.Add("Cache-Control", "no-cache");

        using (Stream requestStream = request.GetRequestStream())
        {
            byte[] postBuffer = Encoding.ASCII.GetBytes(post);
            requestStream.Write(postBuffer, 0, postBuffer.Length);
        }

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader responseReader = new StreamReader(responseStream))
                {
                    string json = responseReader.ReadToEnd();
                    shortUrl = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
                }
            }
        }
    }
    catch (Exception ex)
    {
        // if Google's URL Shortner is down...
        Utility.LogSave("UrlShorten", "Google's URL Shortner is down", url, ex.ToString());
        //System.Diagnostics.Debug.WriteLine(ex.Message);
        //System.Diagnostics.Debug.WriteLine(ex.StackTrace);
    }
    return shortUrl;
}

I have created a scheduler to shorten large number of urls. 我创建了一个调度程序来缩短大量的URL。 And most of the time of got below exception 并且大部分时间都低于例外

System.Net.WebException: The remote server returned an error: (403) Forbidden. System.Net.WebException:远程服务器返回错误:(403)Forbidden。 at System.Net.HttpWebRequest.GetResponse() 在System.Net.HttpWebRequest.GetResponse()

I was thinking that due to Courtesy Limit i was getting this exception so increased Per-User Limit by 100,000.0 requests/second/user but still I'm getting the same exception. 我在想,由于Courtesy Limit我得到了这个例外,所以每用户限制增加了100,000.0请求/秒/用户,但我仍然得到相同的例外。

I don't understand why its happening even i am making hardly 2000 request to the server at a time. 我不明白为什么它发生,即使我一次几乎没有2000服务器的请求。

Please advise. 请指教。

Looking at your question I presume it's a rate-limit exceeded error. 看看你的问题,我认为这是一个超出速率限制的错误。 You can retrieve the error response if you modify your code like this: 如果您修改代码,则可以检索错误响应:

try
{
  ........
}
catch (WebException exception)
{
   string responseText;

   using(var reader = new StreamReader(exception.Response.GetResponseStream()))
   {
     responseText = reader.ReadToEnd();
   }
}
catch (Exception ex)
{
  ......
}

If it's a rate limit exceeded error you will find something like this in responseText : 如果超出速率限制错误,您将在responseText找到类似的内容:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "rateLimitExceeded",
    "message": "Rate Limit Exceeded"
   }
  ],
  "code": 403,
  "message": "Rate Limit Exceeded"
 }
}

This error occuring because your posting the data to webservice very fastly so google detecting it as robot.(if you post data manually this frequently it will ask to enter captcha code ). 发生此错误是因为您将数据快速发布到webservice,因此谷歌将其检测为机器人。(如果您经常手动发布数据,则会要求输入验证码)。

so to resolve this problem you need to increase time interval between each post request. 所以要解决这个问题,你需要增加每个帖子请求之间的时间间隔。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Net.WebException:远程服务器返回错误:(403)访问Google API时禁止访问 - System.Net.WebException: The remote server returned an error: (403) Forbidden when access Google API System.Net.WebException:远程服务器返回错误:(403)禁止 - System.Net.WebException: The remote server returned an error: (403) Forbidden System.Net.WebException:远程服务器返回错误:(403)禁止 - System.Net.WebException : The remote server returned an error: (403) Forbidden 无法获取Web HTML代码:System.Net.WebException:&#39;远程服务器返回错误:(403)禁止。 - Can't get web html code: System.Net.WebException: 'The remote server returned an error: (403) Forbidden. System.Net.WebException:&#39;远程服务器返回错误:(400)错误的请求。 尝试从Google服务器下载数据时 - System.Net.WebException: 'The remote server returned an error: (400) Bad Request.' when trying to download data from google server IIS asp.net C# API Post Method 错误“&#39;远程服务器返回错误:(403) Forbidden。&#39;” - IIS asp.net C# API Post Method error "'The remote server returned an error: (403) Forbidden.'" 如何捕获“远程服务器返回错误:(403)禁止。”的异常。 - how to catch the exception for “The remote server returned an error: (403) Forbidden.” System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() 远程服务器返回错误:(403)禁止。 在发帖期间…? - The remote server returned an error: (403) Forbidden. during post request…? WebClient:下载未完成:远程服务器返回错误:(403)禁止。 - WebClient : Download Not Complete: The remote server returned an error: (403) Forbidden.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM