简体   繁体   中英

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. 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. If asp.net is not your speciality, don't worry, I was just wondering if there was anything glaringly obvious there as well.

I also had to create an asp.net website to post data to the Windows Forms application. 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.

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. 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! watch.jpg

As you can see, I am getting System.NotSupported on datastream.Length and datastream.Position

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?:

    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.

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:

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

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

I get the following response back: System.Byte[]

This is not a variable, but a string containing the arbitrary words and symbols 'System.Byte[]'

Something is not working (obviously) I then see this System.NotSupportedException in my Watch window. 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.

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. 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. You simply cannot use the Length property on a NetworkStream .

Let me try to simplify your code in case you really want to use 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?

Try using the NameValueCollection , just as Darin said, and instead of using a byte array do as follows:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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