2

In a web page that I'm developing (using asp.net, but that is not relevant to this question I think) I have the necessity of making an HTTP GET request to an external website.

The two problems I have are these:

1) The request must be made client-side, because the website I'm making the request to restricts access to only certain IP addresses. My client will be in the range of accepted IPs, but my server will not, so the request must be made by the client using some kind of client-side script (Javascript or other, suggestions are welcome)

2) The request must include a cookie for the external site with a certain value that I must set, so the method/library/script that makes the request must allow me to set a cookie

I searched on SO and other sites but I'm having little luck, the few examples I found don't seem to work. Any idea in any client-side language is much appreciated.

Master_T
  • 7,232
  • 11
  • 72
  • 144
  • @newfurniturey cookies are fixed for domains, since he is doing a cross domain call the cookie wont be sent because its not for the domain. see this question (i dont know if the answer will help): http://stackoverflow.com/questions/16851896/how-to-set-cookie-value-with-ajax-request – x4rf41 Nov 27 '13 at 16:46
  • A Java applet, Flash or ActiveX control *might* make this possible but with limited browser support and nasty security warnings. I'd strongly recommend removing the cookie requirement and using some other mechanism to convey the data. – Quentin Nov 27 '13 at 16:51
  • @php_nub_qq: any good examples? – Master_T Nov 27 '13 at 16:54
  • @Quentin: unfortunately I have no control over the server :( security warnings are not a concern in this case, as long as it works in Chrome (Mobile version) it would be ok. – Master_T Nov 27 '13 at 16:56
  • Mobile Chrome? Oh, well that rules out Java, Flash and ActiveX. You'd probably be best off writing a native application instead of using the browser. – Quentin Nov 27 '13 at 17:23
  • @Master_T http://www.webdevstuff.com/86/javascript-xmlhttprequest-object.html – php_nub_qq Nov 27 '13 at 18:20
  • @php_nub_qq — Doesn't let you set a cookie, so it isn't suitable. Also likely to run into the Same Origin Policy. – Quentin Nov 27 '13 at 21:11
  • @Quentin I thought you should be able to set cookies with a header, like your browser does when sending regular http requests. Why isn't that available? – php_nub_qq Nov 27 '13 at 21:35
  • You'll get a `Refused to set unsafe header "Cookie"` error. It is one of the headers that require the request to be terminated: http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader()-method – Quentin Nov 27 '13 at 21:41

1 Answers1

-1

With jQuery you can send and HTTP GET request in a really simple way, chek this reference

Something like:

$.get( "test.php", { your_cookie_value: "111111" } );
Guillem Cucurull
  • 1,681
  • 1
  • 22
  • 30
  • 3
    That will set a *query string* not a cookie. Also, assuming that GET is not being abused, the JavaScript will need to read the response and (under default circumstances) the Same Origin Policy will prevent that. – Quentin Nov 27 '13 at 16:48
  • 1
    As Quentin points out, this doesn't set a cookie, it passes a QueryString :( – Master_T Nov 27 '13 at 16:57
  • Yeah I see, maybe using $.ajax() you could use withCredentials field, but I don't think this would work http://stackoverflow.com/questions/14462423/cross-domain-post-request-is-not-sending-cookie-ajax-jquery – Guillem Cucurull Nov 27 '13 at 17:24