【问题标题】:Stuck with httpwebrequest usage卡住了 httpwebrequest 的使用
【发布时间】:2012-06-04 14:42:29
【问题描述】:

我对使用 httpwebrequest 有点困惑。我试图查看一些文章,但由于我是第一次这样做,所以无法从中获得太多。以下是我正在尝试处理的代码,我有几个问题,

a) ASPX 页面定义了一些控件,并且在代码隐藏中我创建了一些控件。当我使用 POST 执行 httpwebrequest 时,是否需要考虑所有控件及其值?我只需要为其中一个控件执行 POST。我可以只为那个控件做吗?

b) 在“(HttpWebRequest)WebRequest.Create”中应该指定什么 URL?我假设它是向用户显示的同一页面。例如,在下面的示例中是 ("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");

c) 为了实现 httpwebrequest,我还需要在标记或代码中修改或处理任何其他内容吗?

    private void OnPostInfoClick(object sender, System.EventArgs e)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData =  ""; //Read from the stored array and print each element from the array loop here. 
    byte[] data = encoding.GetBytes(postData);

    // Prepare web request...
    HttpWebRequest myRequest =
      (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");
    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();
    // Send the data.
    newStream.Write(data, 0, data.Length);
    newStream.Close();
}

【问题讨论】:

    标签: c# asp.net url httpwebrequest http-post


    【解决方案1】:

    通常只会将它用于服务器到服务器的通信(不一定用于页面到页面的数据传输)。

    如果我正确理解了您的帖子和问题,您似乎只是想将 POST 数据发送到您的 ASP.Net 应用程序中的其他页面。如果是这样,您可以这样做的一种方法是简单地更改提交按钮(表单目标)的PostBackUrl,而不是正常的回发(到同一页面)。

    还有其他方法,不过这个应该是最简单的。

    <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="foo.aspx" />
    

    在上面,POST 将被发送到foo.aspx,而不是返回给自身,您可以在其中检查/使用/处理 POST 的数据。


    根据您的评论更新:

    您不必为此编写 HttpWebRequest 代码。正常的 ASP.net WebForms 模型会为您完成。

    鉴于这个简单的 ASP.net Web 表单页面:

    <form id="form1" runat="server">
    
    Coffee preference:
    <asp:RadioButtonList ID="CoffeeSelections" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
       <asp:ListItem>Latte</asp:ListItem>
       <asp:ListItem>Americano</asp:ListItem>
       <asp:ListItem>Capuccino</asp:ListItem>
       <asp:ListItem>Espresso</asp:ListItem>
    </asp:RadioButtonList>
    
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    ....
    </form>
    

    您的内联或后面的代码如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
       //do whatever you want to do on Page_Load
    }
    
    
    protected void Button1_Click(object sender, EventArgs e)
    {
       //handle the button event
       string _foo = CoffeeSelections.SelectedValue;
       ...
    }
    

    【讨论】:

    • 感谢您的回复。不,我不是要发布到另一个页面,而是发布到同一页面。基本上在页面的 Onload 中,我调用 httpwebrequest,我希望与 POST 数据一起显示同一页面。
    • 那么你就没有什么“额外”的事情要做了。您可以处理回发数据(这是默认的 asp.net 网络表单行为)。
    • 所以在上面的例子中,我该如何处理:我有一个在运行时生成的图像控件,而 imageUrl 太长了。因此,我将采用 POST 方法。但是,当显示页面时,它会正确显示图像吗?上面的代码哪里需要提到那个image-control?
    • 更新了答案以显示您如何处理 POST 数据(一种方式)。如果您愿意,还可以检查请求数据。提供有关您的“图片网址太长”的更多详细信息,或者如果上述概念不能完全让您到达那里,请创建一个新问题...
    • 谢谢,但这就是我正在做的事情。在 OnLoad() 中,我创建 imageControl 并将 XML 文件中的 URL 分配给它。然后我解析 URL,仍然发现 URL 长度更长(超过 3000 个字符)。然后我创建一个数组,数组元素基本上是来自 URL 的值,每个“&”是一个数组元素。然后我循环该数组并在上面的 httpwebrequest 代码中创建 POSTDATA。所以我的困惑是当 POST 数据完成并显示我的页面时,图像将如何显示以及图像的 URL 值将是什么?
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 2021-10-08
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多