title: Getting System.Net.WebException: The remote server returned an error: (403) Forbidden. in Google URL Shortener API Call
body: <p>I am using below code to shorten long urls</p>
  
  <pre><code>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"": ?""(?&lt;id&gt;.+)""").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;
  }
  </code></pre>
  
  <p>I have created a scheduler to shorten large number of urls. And most of the time of got below exception</p>
  
  <p>System.Net.WebException: The remote server returned an error: (403) Forbidden.     at System.Net.HttpWebRequest.GetResponse()</p>
  
  <p>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.</p>
  
  <p>I don't understand why its happening even i am making hardly 2000 request to the server at a time.</p>
  
  <p>Please advise.</p>