에 나는 긴 URL 내가 URL을 많은 수를 단축하기 위해 스케줄러를 만든 System.Net.WebException 받기 : 원격 서버에서 오류를 반환했습니다 : (403) 금지되었습니다. 구글 URL Shortener를 API 호출
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;
}
을 단축 코드 아래 사용하고 있습니다. 그리고 대부분의 시간이 예외보다 아래에있어
System.Net.WebException : 원격 서버가 오류를 반환했습니다. (403) 금지되었습니다.
나는 예외 제한 때문에 100,000.0 요청/초/사용자에 의해 증가 된이 사용자 예외 제한을 받고 있지만 여전히 동일한 예외가 발생한다고 생각했습니다.
나는 왜 그런 일이 발생하는지조차도 한 번에 서버에 거의 2000 건의 요청을하지 못한다.
알려 주시기 바랍니다.
전체 json 응답이 무엇인지 확인할 수 있습니까? 상태 코드뿐만 아니라 – Erwin
"(HttpWebResponse response = (HttpWebResponse) request.GetResponse())"를 사용하여 예외를 throw하고 완전한 실행은 "System.Net.WebException : 원격 서버가 오류를 반환했습니다 : (403) Forbidden at System.Net D : \ Dotnet Projects \ FutureZoom \ BusinessLogic \ Utility.cs의 BusinessLogic.Utility.UrlShorten (String url)에서 .HttpWebRequest.GetResponse() \t : 행 140 " –