0

I create website at visual studio 2010. So, i should make open a new form and send information from first form. I used text file (i write from fist page to file and read this file at new form) and this is worked. But i want created connection by GET/POST request. I get this code from How to make an HTTP POST web request. Project is compile, but exceeds the time limit. So, bottom i attached code and error.

Code from first page

var request = (HttpWebRequest)WebRequest.Create("http://localhost:55590/WebSite2/Form2.aspx");

    var postData = text;
    var data = Encoding.ASCII.GetBytes(postData);

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;

    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Code from second page

var request = (HttpWebRequest)WebRequest.Create("http://localhost:55590/WebSite2/Form2.aspx");

    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Error

Operation timed out
Description: An unhandled exception occurred while executing the current web request. Examine the stack trace for more information about this error and the code snippet that caused it.

Exception Details: System.Net.WebException: The operation timed out

Source error:
136:        }
137:
138:        var response = (HttpWebResponse)request.GetResponse();  // Error here
139:
140:        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

I tried and second variant from source, but get error. So, help me please

OKIS
  • 53
  • 1
  • 8
  • 1
    Are you processing some text file and looking to then pass results after doin so? Or are you thinking that you going to use a text file to pass some information from the first page to the 2nd page - as that is a VERY VERY bad idea and makes no sense at all (what if 10 people are using the site - they going to trample on each other as you write out the text file and then try to pass to the next page. If you have some data, some controls, some check boxes, text boxes etc, then a truckload of ways exist to pass and use that information in the 2nd form - but a text file is simply not the approach – Albert D. Kallal Feb 20 '21 at 23:12
  • @AlbertD.Kallal I used text file for send data to 2nd page. I know, that this is bad idea... Thank you :) – OKIS Feb 21 '21 at 08:38
  • No problem - see my answer - the ability to pass the previous page and values to the next page is a built in feature of asp.net. See my post - it explains how this works. – Albert D. Kallal Feb 21 '21 at 19:24

2 Answers2

1

So there are quite a few ways to send data and "things" from one web page to the next.

Session() is certainly one possible way.

Another is to use parameters in the URL you thus often see that on many web sites

Even as I write this post - we see the URL on StackOverFlow as this:

stackoverflow.com/questions/66294186/http-request-get-post?noredirect=1#comment117213494_66294186

So, the above is how stack overflow is passing values.

So session() and paramters in the URL os common.

However, asp.net net has a "feature" in which you can pass the previous page to the next. So then it becomes a simple matter to simply pluck/get/grab/use things from that first page in the next page you loaded. so this feature is PART of asp.net, and it will do all the dirty work of passing that previous page for you!!!

Hum, I wonder if people have to ever pass and get values from the previous page? I bet this most common idea MUST have been dealt with, right? And not only is this a common thing say like how human breathe air? It also a feature of asp.net.

So, a REALLY easy approach is to simply when you click on a button, and then jump to the next page in question? Well, if things are setup correctly, then you can simple use the "previous" page!!!

You can do this on page load:

if (IsPostBack == false)
{
    TextBox txtCompay = PreviousPage.FindControl("txtCompnay");
    Debug.Print("Value of text box company on prevous page = " + txtCompay.Text);
}

This approach is nice, since you don't really have to decide ahead of time if you want 2 or 20 values from controls on the previous page - you really don't care.

How does this work?

The previous page is ONLY valid based two approaches.

First way:

The button you drop on the form will often have "code behind" that of course jumps or goes to the next page in question.

That command (in code behind) is typical this:

Response.Redirect("some aspx web page to jump to")

The above does NOT pass previous page

However, if you use this:

Server.Transfer("some aspx web page to jump to")

Then the previous page IS PASSED and you can use it!!!!

So in the next page, on page load event, you can use "prevouspage" as per above.

so Server.Transfer("to the next page") WILL ALLOW use of "previous page" in your code.

So you can pick up any control, any value. You can even reference say a gridview and the row the user has selected. In effect the whole previous page is transferred and available for using in "previous page" as per above. You can NOT grab viewstate, but you can setup public methods in that previous page to expose members of viewstate if that also required.

You will of course have to use FindControl, but it is the previous page.

The other way (to allow use of previous page).

You don't use code behind to trigger the jump to the new page (with Server.Transfer()), but you set the post-back URL in the button in that first page. that is WHAT the post-back URL is for!!! (to pass the current page to the post-back URL).

eg this:

   <asp:Button ID="Button1" runat="server" Text="View Hotels"
    PostBackUrl="~/HotelGrid.aspx" />

So you use the "post back" URL feature of the button.

Now, when you click on that button, it will jump to the 2nd page, and once again previous page can be used as per above. And of course with post-back URL set, then of course you don't need a code behind stub to jump to that page.

So this is quite much a "basic" feature of asp.net, and is a built-in means to transfer the previous page to the next. Kind of like asp.net "101".

So this perhaps common, in fact MOST COMMON BASIC need to pass values from a previous web page is not only built in, but it is in fact called "prevous page"!!!!!

Rules:

Previous page only works if you use a Server.Transfer("to the page")

Response.Request("to the page") does NOT allow use of previous page.

using the post-back URL of a button (or in fact many other controls) also
have a post-back URL setting - and again if that control has post-back URL, then
again use of previous page is allowed due to that control causing such page
navagation.

The previous page can ONLY be used on the first page load (ispostBack = False).

Using post-back URL in a button of course means a code behind stub is not required for the page jump. And once again, using post-back URL will ensure that page previous can be used in the next page.

However, in those cases in which you don't want to hard code the URL, or perhaps some additonal logic occurs in that button code stub before such navigation to the next page? (or is even to occur??).

Then ok post-back URL is not all that practical, but you can then resort to and use Server.Transfer() in that code behind, and AGAIN this allows use of the built in "previous page".

Just keep in mind that whatever you need/want/will grab from the previous page HAS to occur on the FIRST PAGE load of that page we jumped to. Any additonal button post back and the regular life cycle and use of controls and events in code behind on that page will NOT have use of previous page AFTER the first page load has occurred. (previous page will be null and empty).

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
0

You can try it that way.

var request = (HttpWebRequest)WebRequest.Create("http://localhost:55590/WebSite2/Form2.aspx");

var postData = text;
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

 HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
 using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
 {
     result = streamReader.ReadToEnd();
 }
  • Thank you for hint. I tried this method, but get error on this line: HttpWebResponse httpResponse = (HttpWebResponse)HttpWebRequest.GetResponse(); As i look, this command supported to version of my framework (watch here https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=net-5.0). But i get error: An object reference is required for the non-static field, method, or property 'System.Net.WebRequest.GetResponse()' – OKIS Feb 21 '21 at 09:02