简体   繁体   English

HTTP 请求 GET/POST

[英]HTTP request GET/POST

I create website at visual studio 2010. So, i should make open a new form and send information from first form.我在 Visual Studio 2010 创建网站。所以,我应该打开一个新表单并从第一个表单发送信息。 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.但我想通过 GET/POST 请求创建连接。 I get this code from How to make an HTTP POST web request .我从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所以,请帮助我

So there are quite a few ways to send data and "things" from one web page to the next.因此,有很多方法可以将数据和“事物”从一个 web 页面发送到下一个页面。

Session() is certainly one possible way. Session() 当然是一种可能的方式。

Another is to use parameters in the URL you thus often see that on many web sites另一个是使用 URL 中的参数,因此您经常在许多 web 站点上看到

Even as I write this post - we see the URL on StackOverFlow as this:即使在我写这篇文章的时候——我们在 StackOverFlow 上看到 URL 是这样的:

stackoverflow.com/questions/66294186/http-request-get-post?noredirect=1#comment117213494_66294186 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.所以 URL 操作系统中的 session() 和参数是通用的。

However, asp.net net has a "feature" in which you can pass the previous page to the next.但是,asp.net 网络有一个“功能”,您可以在其中将上一页传递到下一页。 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!!!所以这个功能是 asp.net 的一部分,它会为你完成所有通过前一页的脏活!!!

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.它也是 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.这种方法很好,因为您不必提前决定是否要从前一页的控件中获取 2 或 20 个值 - 您真的不在乎。

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!!!!然后上一页就PASSED了,可以用了!!!

So in the next page, on page load event, you can use "prevouspage" as per above.因此,在下一页中,在页面加载事件中,您可以按照上述使用“prevouspage”。

so Server.Transfer("to the next page") WILL ALLOW use of "previous page" in your code.所以 Server.Transfer("to next page") 将允许在您的代码中使用“上一页”。

So you can pick up any control, any value.所以你可以选择任何控制,任何价值。 You can even reference say a gridview and the row the user has selected.您甚至可以参考 gridview 和用户选择的行。 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.您当然必须使用 FindControl,但它是前一页。

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.您不使用后面的代码来触发跳转到新页面(使用 Server.Transfer()),但是您在第一页的按钮中设置了回发 URL。 that is WHAT the post-back URL is for.!!这就是回发 URL 的用途。!! (to pass the current page to the post-back URL). (将当前页面传递给回发 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.因此,您使用按钮的“回发”URL 功能。

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.当然,如果设置了回发 URL,那么您当然不需要存根后面的代码来跳转到该页面。

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.因此,这在很大程度上是 asp.net 的“基本”功能,并且是将上一页转移到下一页的内置方法。 Kind of like asp.net "101".有点像 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"!!!!!所以这可能很常见,实际上最常见的 BASIC 需要从以前的 web 页面传递值不仅是内置的,而且它实际上被称为“前一页”!!!!!

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.在按钮中使用回发 URL 当然意味着页面跳转不需要存根代码。 And once again, using post-back URL will ensure that page previous can be used in the next page.再一次,使用 post-back URL 将确保可以在下一页使用上一页。

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?但是,在您不想对 URL 进行硬编码的情况下,或者在导航到下一页之前,该按钮代码存根中可能会发生一些附加逻辑? (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".然后确定回发 URL 并不是那么实用,但是您可以在后面的代码中求助并使用 Server.Transfer() ,并且再次允许使用内置的“上一页”。

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). (上一页将为 null 并且为空)。

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();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM