使用POST方法将数据从WinForm传递到WebForm并检索它



有谁知道一种简单的方法,通过'POST'方法从Windows表单应用程序发送intstringbyte[],并在 ASP.NET WebForm中获取这些数据?

所以,事实上:

Form1 (data) -→ (data) WebPage1.aspx

这是我的代码中现在的内容:客户端 :

    String idtostring = "id=" + opid.ToString();
    intarray = Encoding.ASCII.GetBytes(idtostring);
    startlog(intarray);
    private void startlog(byte[] array)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:51146/MyPage.aspx");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = array.Length;
        var Url = "http://localhost:51146/MyPage.aspx";
        wb.Navigate(Url, true);
        Stream postdata = request.GetRequestStream();
        postdata.Write(array, 0, array.Length);
        postdata.Close();
    }

服务器端 :

    private String getpostdata()
    {
        return Request.Form["id"];
    }

这将返回空...我不知道问题是在客户端还是在服务器端。我需要做的是通过POST方法从客户端发送一个看起来像"id=7"的数据,并将其取回服务器上。我正在使用自定义对用户进行身份验证的 ID,因此我需要在 Web 浏览器中显示将对用户进行身份验证的会话。

更新:我不确定Request.Form是否适合观看...

请参阅此图像,其中显示了我通过断点获得的内容(我是新手,因此暂时无法发布图像)→ https://i.stack.imgur.com/3VTyL.png

我刚刚尝试了这个:

    private void startlog(byte[] array)
    {
        var Url = "http://localhost:51146/MyPage.aspx";
        wb.Navigate(Url, "_blank", array, "");
    }

结果完全一样...

下面是一个字符串示例。

var helloWorldString="HelloWorldKey=HelloWorldValue";
var array= Encoding.ASCII.GetBytes(helloWorldString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:51146/MyPage.aspx");
request.Method = "POST";
request.ContentLength = array.Length;
request.ContentType = "application/x-www-form-urlencoded";
var Url = "http://localhost:51146/MyPage.aspx";
using(Stream postdata = request.GetRequestStream())
{
    postdata.Write(array, 0, array.Length);
}

在服务器上,您将使用 Request.Form["HelloWorldKey"] 读取它;

整数更棘手。在客户端上将它们转换为字符串,然后将它们解析为服务器上的整数。

字节数组也是如此。在客户端上将它们转换为 Base 64 字符串,然后在服务器上将它们转换回字节数组。

最好使用 HttpUtility 对字符串值进行编码,而不是手动执行此操作。有关详细信息,请参阅此答案。

最新更新