How to simulate a visit to a url?

I have a page where the user submits the order, and after sending it, I want to click the URL ( http://externalsite.com?id=12345&sessionid=abc123), without actually redirecting them to an external page.

Is there any way to do this?

+5
source share
4 answers

Of course, use HttpWebRequestfrom your server side code. Here is an example:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    "http://externalsite.com?id=12345&sessionid=abc123");
request.Method = "GET";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    string result = reader.ReadToEnd();
    // Process the response text if you need to...
}
+8
source

If you need the user's cookies (login details and other user settings) on http://externalsite.com/, you can insert <iframe>or or use the ajax request from the user’s browser.

Using <iframe>:

<iframe src="http://externalsite.com?id=12345&sessionid=abc123" width="1" height="1" frameborder="0"></iframe>

"" ( ):

<img src="http://externalsite.com?id=12345&sessionid=abc123" width="1" height="1" />

jQuery - ajax :

$.ajax({
    url: "http://externalsite.com?id=12345&sessionid=abc123"
});

, iframe , javascript, .

+5

WebClient HTTP- Asp.Net . , , html.

+2

, voithos Joel Purra, . :

1) - . voithos :

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    "http://externalsite.com?id=12345&sessionid=abc123");
request.Method = "GET";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    string result = reader.ReadToEnd();
    // Process the response text if you need to...
}

, . , HTTP-. , , . , /, : , - - , , , .

2) , HTML , HTML-, .

<div style="display: none;">
 <iframe src="http://externalsite.com?id=12345&sessionid=abc123"></iframe>
</div>

, - , 404 .., - , .

3) , ​​ jQuery, , , . :

<script type="text/javascript">
    $(document).bind('ready', function () {
        $('#formSubmitButton').bind('click', function (ev) {
            ev.preventDefault();  // These two lines stop the default processing from
            ev.stopPropagation(); // occurring on form-submit (i.e. no full post-back)

            // This line starts an asynchronous call to the server, posting your form
            // data itself.
            $.ajax({
               url: '/My/Post/Url',
               type: 'POST',
               async: false,

               // You could use a library for this kind of form parsing. I suggest
               // http://www.json.org/js.html - for serialization, and 
               // http://code.google.com/p/form2js/ - for form conversion. It great.
               data: { my: 'form', data: 'fields' },

               success: function (data) {
                   $.ajax({
                       url: '/The/External/Url',
                       type: 'POST',
                       async: false,

                       data: { external: 'data', goes: 'here' },
                       success: function (remoteData) {
                           if (remoteData) // validate response here
                               displaySuccess();
                           else
                               displayFailure();
                       },
                       error: displayFailure
                   });
               },
               error: displayFailure
            });
        });
    });
</script>

- - . , , /, - , , , , , .

, , - , JavaScript, .

0

All Articles