远程服务器返回错误:(403)禁止..在谷歌短网址

时间:2017-06-13 05:28:07

标签: c# asp.net

 public string GetShortURL(string longUrl)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.googleapis.com/urlshortener/v1/url?key=My_API_Key");
        request.Method = "POST";           
        request.ContentType = "application/json";
        string requestData = string.Format(@"{{""longUrl"": ""{0}""}}", longUrl);
        byte[] requestRawData = Encoding.ASCII.GetBytes(requestData);
        request.ContentLength = requestRawData.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(requestRawData, 0, requestRawData.Length);
        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader responseReader = new StreamReader(response.GetResponseStream());
        string responseData = responseReader.ReadToEnd();
        responseReader.Close();

        return responseData;
    }

我试图通过传递我的原始网址字符串来创建短网址,但是它提供了一个例外

enter image description here

0 个答案:

没有答案