简体   繁体   English

将数据发布到asp.net网页时出现问题(Desktop C#end中的问题)

[英]Problems POSTing data to an asp.net webpage (problem in Desktop C# end)

Thank you so much for taking the time to read my post! 非常感谢您花时间阅读我的帖子! I am having a problem POSTing data from a C# Desktop application to a C# asp.net web page. 我在将数据从C#Desktop应用程序发布到C#asp.net网页时遇到问题。 I believe that the problem lies in the Desktop application (or at least one of the problems does!) I will also post the asp.net code I am using. 我相信问题在于桌面应用程序(或者至少有一个问题!)我还会发布我正在使用的asp.net代码。 If asp.net is not your speciality, don't worry, I was just wondering if there was anything glaringly obvious there as well. 如果asp.net不是你的专长,请不要担心,我只是想知道那里是否还有明显的东西。

I also had to create an asp.net website to post data to the Windows Forms application. 我还必须创建一个asp.net网站,将数据发布到Windows窗体应用程序。 This is working perfectly. 这很完美。

Here is the code I am using. 这是我正在使用的代码。 What is not working is discussed below. 下面讨论了什么不起作用。 I am very bad at all this asp.net stuff, so any help you can provide would be very much appreciated. 我对所有这些asp.net的东西都很糟糕,所以你能提供的任何帮助都会非常感激。

if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() && result == DialogResult.Yes)
            {
                string test = "Test";
                WebRequest request = WebRequest.Create("http://localhost/test.aspx");
                byte[] byteArray = Encoding.UTF8.GetBytes(test);

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

                // Give the response
                using (Stream datastream = request.GetRequestStream())
                {
                    datastream.Write(byteArray, 0, byteArray.Length);
                }
             }

However, when I Debug the application, and put a breakpoint just after datastream.Write() I get some errors in the Variable Watch window. 但是,当我调试应用程序,并在datastream.Write()之后放置一个断点时,我在Variable Watch窗口中出现了一些错误。 I do not get any exceptions except in there. 除了在那里,我没有任何例外。

I cannot upload an image to this website it seems, so I shall upload it to a FreeWebs site - sorry, really embarrassing! 我似乎无法将图像上传到这个网站,所以我将它上传到FreeWebs网站 - 抱歉,真的很尴尬! watch.jpg watch.jpg

As you can see, I am getting System.NotSupported on datastream.Length and datastream.Position 如您所见,我在datastream.Length和datastream.Position上获得System.NotSupported

Could you please help me to fix this? 你能帮我解决这个问题吗? Thanks! 谢谢!

Just in case an asp.net programmer also sees this, is there any problem with this receiving code?: 为了防止asp.net程序员也看到这个,这个接收代码有什么问题吗?:

    protected void Page_Load(object sender, EventArgs e)
    {
        string test = Request.BinaryRead(Request.TotalBytes).ToString();
    }

Thank you all, so, so much for your time! 谢谢大家,非常感谢你的时间!

Richard 理查德

EDIT: In relation to gandjustas's comment, I am providing more information. 编辑:关于gandjustas的评论,我提供更多信息。

Something in the chain is not working. 链中的东西不起作用。 I am not getting any formal exceptions to report. 我没有得到任何正式的例外报道。

If I use this code in the asp.net webpage: 如果我在asp.net网页中使用此代码:

 string test = Request.BinaryRead(Request.TotalBytes).ToString();

        Response.Clear();
        Response.Write(test);
        Response.End();

I get the following response back: System.Byte[] 我收到以下回复:System.Byte []

This is not a variable, but a string containing the arbitrary words and symbols 'System.Byte[]' 这不是变量,而是包含任意单词和符号'System.Byte []'的字符串

Something is not working (obviously) I then see this System.NotSupportedException in my Watch window. 有些东西不起作用(显然)我在Watch窗口中看到了这个System.NotSupportedException。 This make me think that there are two errors: This System.NotSupportedException needs fixing in my C# Desktop Application, and my asp.net webpage should not be displaying System.Byte[] before I have even sent my POST from the application. 这让我觉得有两个错误:这个System.NotSupportedException需要在我的C#桌面应用程序中修复,而我的asp.net网页在我从应用程序发送POST之前不应该显示System.Byte []。

I kind of need help. 我需要帮助。 Thanks! 谢谢!

Couple of remarks about your code: 关于您的代码的几点评论:

  1. You are setting application/x-www-form-urlencoded content type but you are sending some arbitrary byte array. 您正在设置application/x-www-form-urlencoded内容类型,但您正在发送一些任意字节数组。 When you set this content type the server will expect that the request is encoded using it. 设置此内容类型时,服务器将期望使用它对请求进行编码。
  2. The NotSupportedException you are getting in the Debug window are normal. 您在调试窗口中获得的NotSupportedException是正常的。 You simply cannot use the Length property on a NetworkStream . 您根本无法在NetworkStream上使用Length属性。

Let me try to simplify your code in case you really want to use application/x-www-form-urlencoded : 如果您真的想使用application/x-www-form-urlencoded请尝试简化您的代码:

Client: 客户:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key1", "value1" },
        { "key2", "value2" },
    };
    byte[] result = client.UploadValues("http://example.com/test.aspx", values);
}

Server: 服务器:

protected void Page_Load(object sender, EventArgs e)
{
    string key1 = Request["key1"];
    string key2 = Request["key2"];
}

try this 尝试这个

           string test = "Test"; 
            WebRequest request = WebRequest.Create("http://localhost/test.aspx"); 

            request.Method = "POST"; 
            request.ContentType = "text/xml;charset=utf-8"; 
            request.ContentLength = test.Length; 

           using (StreamWriter paramWriter = new StreamWriter(request.GetRequestStream()))
           {
              paramWriter.Write(test, 0, test.Length);

           }

           WebResponse wres = request.GetResponse();
           StreamReader sr = new StreamReader(wres.GetResponseStream());
           string outdata = sr.ReadToEnd().Trim();

You seem to be using MSDN's HowTo on WebRequest class, is that correct? 您似乎在WebRequest类上使用MSDN的HowTo ,这是正确的吗?

Try using the NameValueCollection , just as Darin said, and instead of using a byte array do as follows: 尝试使用NameValueCollection ,就像Darin所说的那样,而不是使用字节数组,如下所示:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key1", "value1" },
        { "key2", Convert.ToBase64String(File.ReadAllBytes(test)) },
    };
    byte[] result = client.UploadValues("http://example.com/test.aspx", values);
}

You can also try changing the content-type; 您也可以尝试更改内容类型; please check the examples here and here . 请查看此处此处的示例。

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

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