Use response redirect in html

Response.Redirect with POST instead of Get?

We have the requirement to take a form submission and save some data, then redirect the user to a page offsite, but in redirecting, we need to «submit» a form with POST, not GET. I was hoping there was an easy way to accomplish this, but I’m starting to think there isn’t. I think I must now create a simple other page, with just the form that I want, redirect to it, populate the form variables, then do a body.onload call to a script that merely calls document.forms[0].submit(); Can anyone tell me if there is an alternative? We might need to tweak this later in the project, and it might get sort of complicated, so if there was an easy we could do this all non-other page dependent that would be fantastic. Anyway, thanks for any and all responses.

I think this is the easy answer you were looking for. I couldn’t believe how ingenious it is. stackoverflow.com/a/6062248/110549

@BrianWarshaw I find System.Net.Http.HttpClient msdn.microsoft.com/en-us/library/… very intuitive and quick to use.

14 Answers 14

Doing this requires understanding how HTTP redirects work. When you use Response.Redirect() , you send a response (to the browser that made the request) with HTTP Status Code 302, which tells the browser where to go next. By definition, the browser will make that via a GET request, even if the original request was a POST .

Another option is to use HTTP Status Code 307, which specifies that the browser should make the redirect request in the same way as the original request, but to prompt the user with a security warning. To do that, you would write something like this:

public void PageLoad(object sender, EventArgs e) < // Process the post on your side Response.Status = "307 Temporary Redirect"; Response.AddHeader("Location", "http://example.com/page/to/post.to"); >

Unfortunately, this won’t always work. Different browsers implement this differently, since it is not a common status code.

Alas, unlike the Opera and FireFox developers, the IE developers have never read the spec, and even the latest, most secure IE7 will redirect the POST request from domain A to domain B without any warnings or confirmation dialogs! Safari also acts in an interesting manner, while it does not raise a confirmation dialog and performs the redirect, it throws away the POST data, effectively changing 307 redirect into the more common 302.

So, as far as I know, the only way to implement something like this would be to use Javascript. There are two options I can think of off the top of my head:

  1. Create the form and have its action attribute point to the third-party server. Then, add a click event to the submit button that first executes an AJAX request to your server with the data, and then allows the form to be submitted to the third-party server.
  2. Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like «Redirecting. «. Then, add a javascript event to the page that submits the form to the third-party server.
Читайте также:  Config php для xenforo

Of the two, I would choose the second, for two reasons. First, it is more reliable than the first because Javascript is not required for it to work; for those who don’t have it enabled, you can always make the submit button for the hidden form visible, and instruct them to press it if it takes more than 5 seconds. Second, you can decide what data gets transmitted to the third-party server; if you use just process the form as it goes by, you will be passing along all of the post data, which is not always what you want. Same for the 307 solution, assuming it worked for all of your users.

Источник

How to Easily Make HTML Redirect to Another Page

TL;DR – HTML redirect takes a website visitor to another site automatically.

Contents

What is an HTML Redirect?

A redirect happens when a user enters a URL, but it changes, and the browser takes them to a different one instead. Website creators rely on them when they need to change the structure of their site or the location of a particular page. Of course, you may redirect to a completely different website as well.

When working with Hypertext Transfer Protocol (HTTP), you need to have a basic understanding of its response codes. They contain three digits, first of which defines their type:

Let’s say you closed your old website and opened a new one. If a user types a URL of the old one into their browser, it will return the response code 404 (Not Found). However, if you use an HTML redirect, the user will get either 301 (Moved Permanently) or 302 (Found). This code is invisible to the user, but the browser understands it and redirects the user to the new URL in moments.

Читайте также:  Pycharm no python interpreters

The Syntax for HTML Redirect Code

The HTML redirect is also known as the meta refresh redirect, or simply HTML meta redirect. It allows you to choose whether you need an immediate or a delayed redirect. If you specify the delay time in seconds, the user will see the old page for exactly that long.

To make a page in HTML redirect to another page, you should follow this syntax:

meta http-equiv="refresh" content="time; URL=new_url" />

As you can see, it requires two parameters:

  • time represents the delay before the browser redirects the user to a different page. Define it in seconds, or enter a 0 if you need an immediate HTML redirect.
  • new_url represents the URL address you need to redirect your user to after the delay.

In the example below, you can see the HTML redirect code that takes the user to BitDegree’s website with a delay of five seconds:

meta http-equiv="refresh" content="5; URL=https://www.bitdegree.org/" />
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion

Why Delay a Redirect in HTML?

If you’re not sure why you should delay your HTML meta redirect, think about a chance to set a message for the user. You could inform them the page has moved, and then promptly send them to the new one.

Another important reason is the slight chance of the tag not being rendered correctly. This might happen if the user is using some ancient browser. In this case, you may add a direct link to the old page which the user might click manually if the HTML redirect code fails.

head> meta http-equiv="refresh" content="5; URL=https://www.bitdegree.org/" /> head> body> p>If you are not redirected in five seconds, a href="https://www.bitdegree.org/">click here a>. p> body>

As you can see, all you need to add a direct clickable link is a pair of anchor tags. Make sure to place it in the section and not the with the HTML meta redirect tag: there is no use for a clickable link that a user cannot see in the first place.

Читайте также:  Python вызвать функцию строкой

HTML Redirect: Useful Tips

  • If you don’t define a new URL address for the redirect, HTML page will simply reload itself after the time specified. It can be useful when you need to refresh dynamic content.
  • We’d advise you to avoid delays shorter than 3 seconds, as that makes it virtually impossible for the user to click the Back button on their browser.
  • Be careful not to overuse HTML meta redirects: if your website has a ton of them, the search engines may think it contains spam and remove it from their index.
  • You can also create redirects with PHP, JavaScript, Ruby on Rails, and Python Flask, as well as in the Apache, Nginx, and Lighttpd web servers.

Источник

Response.Redirect and HTML content

I have a Web Form whose sole purpose is to redirect to other pages. I created it as a normal aspx page and then I deleted everything in the .aspx file and kept just the first line shown below—even the Doctype and HTML tag are gone now:

I also deleted the .designer.cs file as it contained nothing. It works, but I wonder if what I did is right. Are there any concerns about removing all HTML content from the Web Form in this case?

2 Answers 2

None whatsoever. What you have done is perfectly acceptable.

However, if the sole purpose of the pages is to redirect, I would use a Handler/ASHX file as it can be used in exactly the same way and doesn’t have as much overhead as the ASPX page.

If you do Response.Redirect(url) , a redirect header is added and the request is ended. This means that anything in your ASPX page is not output to the client. Any content after Response.Redirect(url) is not output to the page. You can just as well delete it, like you did.

If you do Response.Redirect(url, false) , the response is not ended and your page is output to the client. However, the client never gets to see it because he is redirected.

This is not exactly true — after receiving redirect header, browser will indeed re-direct however if destination page takes time then user can see the html that you have provided earlier. So a good practice would be to have html content that will tell user that he is redirecting to another location (provide a link to the location)!

Thanks! I do Response.Redirect(url, false) as it’s recommended on MSDN, and I imagine that given that there is no HTML content the client will see that same result as if I called Response.Redirect(url) .

Источник

Оцените статью