none
Posting data to a web form in C# RRS feed

  • Question

  • Trying to send data to a web form. It always replies "400".

    I can do it from an html page like so:

        <form action="https://api.particle.io/v1/devices/4500*****3036/Settings?access_token=hidingthispart" method="POST">
            Tell your device what to do!<br>
            <br>
            <input type="text" name="arg" value="" > The settings string.
    
            <br>
            <input type="submit" value="Do it!">
        </form>
    

    But not from .net code like so:

       string postData = "arg=" + tbMaxChamber.Text + "," + tbMinChamber.Text + "," + tbMeatTarget.Text + "," + tbMeatAlert.Text;
                string url = "https://api.particle.io/v1/devices/4500***********63036/Settings?access_token=hidingthispart";
    
                WebRequest request = WebRequest.Create(url);
                request.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
    
                WebResponse response = request.GetResponse();
                lblResponse.Text = (((HttpWebResponse)response).StatusDescription);
    
                dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                lblResults.Text = responseFromServer;
                reader.Close();
                dataStream.Close();
                response.Close();

    Perhaps I have to do something different because it's an https?

    Friday, February 15, 2019 6:27 PM

Answers

  • Wait a minute! I think I asked too soon. The data is getting to the destination and it's the destination that barfs.
    • Marked as answer by Tom Ruby Tuesday, May 21, 2019 6:11 PM
    Friday, February 15, 2019 6:32 PM

All replies

  • Wait a minute! I think I asked too soon. The data is getting to the destination and it's the destination that barfs.
    • Marked as answer by Tom Ruby Tuesday, May 21, 2019 6:11 PM
    Friday, February 15, 2019 6:32 PM
  • I'm not sure you're sending the data in the correct format. A 400 means the data you sent is invalid. This could just be bad inputs but could also mean it didn't like the data you sent. I would recommend you switch to using the far easier WebClient class instead. Then your code can be dramatically reduced to something like this.

    var client = new WebClient();
    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
    //Format the data based upon what the server wants
    var data = "arg=someValue";
    var body = Encoding.ASCII.GetBytes(data);
    var response = client.UploadData(url, body);
    
    //Getting the "string" response that came back
    lblResponse.Text = response;

    You could also use HttpClient which is the preferred approach but requires more changes. Ult

    The underlying problem I think you're having is the conversion to x-www-form-urlencoding. You're just taking raw text and putting it into a string but if that text contains any chars that aren't valid for form encoding then it'll fail. You should probably HTML (or URL) encode the data before converting to the array.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, February 15, 2019 7:10 PM
    Moderator
  • Wait a minute! I think I asked too soon. The data is getting to the destination and it's the destination that barfs.

    HTTP 400 means that the Web server didn't understand the request due to bad data.  
    Friday, February 15, 2019 7:44 PM
  • Hi Tom Ruby,

    Is there any updated? do you resolve the issue?

    If the issue still exists, please feel free let me know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 19, 2019 7:24 AM
    Moderator
  • Yes. The data was getting sent, but the receiving program barfed. Once I fixed the receiver, all was fine.
    Tuesday, May 21, 2019 6:12 PM