0

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 ?

arash rajaei
  • 307
  • 1
  • 2
  • 11
  • What error do you get? – Hatjhie Jul 07 '14 at 03:32
  • it dont gave any error but also the response is nothing or empty . – arash rajaei Jul 07 '14 at 03:33
  • There are two problems in your code: 1. You are expecting the cookies to be returned but you only return HTML. 2. Even so you are expecting information from your return cookies, you can't just do it by doing Response.Write. – Hatjhie Jul 07 '14 at 03:52
  • sorry can I ask you describe me the problems of code ? – arash rajaei Jul 07 '14 at 03:52
  • Check this out: http://stackoverflow.com/questions/3340797/can-an-ajax-response-set-a-cookie I think you might need to change your code architecture. By making one web service callable by AJAX, and that web method return cookies instead. – Hatjhie Jul 07 '14 at 03:56
  • What I mean is this: AJAX code -> your own domain page server -> whatever other domain page server to create the cookies. The return output is: cookies returned from whatever other domain page server to your own page and return back the cookies through AJAX code. – Hatjhie Jul 07 '14 at 03:59

0 Answers0