Sourabh Dhiman

Sourabh Dhiman

  • NA
  • 323
  • 58.8k

Please Any Help Me

Jul 3 2019 6:02 AM
public ActionResult Index()
{
return View();
}
private class GoogleResponse
{
public string longUrl { get; set; }
}
[HttpPost]
public JsonResult GetShortURL(string longUrl)
{
WebRequest request = WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDFhSDbTWk_dcit15wDWtIUG2CTfSM-nnM");
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();
WebResponse response = request.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Models.GoogleResponse>(responseData);
return Json(results.Id);
}
}
}
 
------------------------------------------------------------------------
 
 
<h2>Index</h2>
<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#button1").click(function () {
$.ajax({
type: "POST",
url: "/Home/GetShortURL",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: '{ "longUrl" : "' + $("#text1").val() + '" }',
success: function (results) {
$("#text2").val(results);
Console.log("button1");
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
});
});
});
</script>
<span>Enter Long URL :</span>
<br />
<br />
<input type="text" id="text1" size="60" />
<br /><br />
<input type="button" id="button1" value="Get Short URL"/>
<br /><br />
<input type="text" id="text2" readonly="readonly" size="60"
 
This Code Convert Long Url To Short Url . But i will be face a problem(the remote server returned an error (403) forbidden) .. Please solve my error.
 
 

Answers (1)