I have a JavaScript code that send a POST request to a web Url and gets the response the response contains a string and some cookies , Now I want to convert it to VB.net aspx code so make this action server side because of cross domain issue , I wrote the code but its not working and all response have null body . can I ask for some help ?
JavaScript Code
function goldprice_req(deltas, goldurl) {
(function ($) {
$.ajax('Some URL?_t=' + timenow , {
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: { 'id': goldurl },
cache: false,
type: "POST",
dataType: "json",
contentType: "application/json",
success: goldpriceSuccess,
error: function (xhr, status, error) {
console.log(status);
failure++;
setTimeout(function () { goldprice_req(deltas, goldurl); }, 5000);
if (failure >= 3) {
if (!$('#ajaxfailure').html())
$('#content-top').prepend('<p id="ajaxfailure">Error.</p>');
$('#ajaxfailure').show();
}
}
});
})(jQuery);
}
ASPX VB code
Dim postdt As String = ""
Dim enc As UTF8Encoding
Dim postdatabytes As Byte()
Dim cookie As New CookieContainer()
Dim request1 As HttpWebRequest = HttpWebRequest.Create("Some Url" & timenow )
request1.CookieContainer = cookie
request1.Method = "POST"
enc = New System.Text.UTF8Encoding()
postdatabytes = enc.GetBytes(postdt)
request1.Headers("ContentType") = "application/x-www-form-urlencoded"
request1.ContentLength = postdatabytes.Length
Using stream = request1.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Dim response1 As HttpWebResponse = DirectCast(request1.GetResponse(), HttpWebResponse)
For Each cok As Cookie In response1.Cookies
cookie.Add(cok)
Next
Dim htmlbody As String
Dim resstr As Stream = response1.GetResponseStream
Dim reader As StreamReader = New StreamReader(resstr, True)
While reader.Peek >= 0
htmlbody = reader.ReadToEnd
End While
Response.Write(htmlbody)
response1.Close()
reader.Close()
What is wrong with my code ?