0

I am currently working on implementing a class for accessing rest webservices with my C# program. I managed to do that for PUT, Post and GET, but I have problems with the Patch. The following error occurs everytime:

There is an exception error “System.Net.WebException” in System.ddll

Additional information: remote server reported: (400) Unvalid request

I have researched and tried out various things – to no avail. I would appreciate any help! Many thanks in advance

    public string WebserviceSenden()
    {

        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create( GrundURL + ErweiterungsURL);
        // Set the Method property of the request to POST.
        request.Method = Methode;
        // Create POST data and convert it to a byte array.
        string postData = DateSenden;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/json";      
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }

    public string WebserviceEmpfangen()
    {
        // Create a request for the URL. 
        WebRequest request = WebRequest.Create(
        GrundURL + ErweiterungsURL);
        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams and the response.
        reader.Close();
        response.Close();

        return responseFromServer;
    }
}




private void button1_Click(object sender, EventArgs e)
    {
        String Jason = "";


        RestClient AbfrageStarten = new RestClient();
        AbfrageStarten.GrundURL = "URL";
        AbfrageStarten.ErweiterungsURL = "540697";
        Jason = AbfrageStarten.WebserviceEmpfangen();

        var settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            MissingMemberHandling = MissingMemberHandling.Ignore
        };


        var list = JsonConvert.DeserializeObject<List<Lagereinheit>>(Jason, settings);

        KorrekturLagereinheit  = list[0];

        KorrekturLagereinheit.stueckzahl = 5858;


        string Test;
        Test = JsonConvert.SerializeObject(KorrekturLagereinheit, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });


        RestClient AntwortSenden = new RestClient();
        AntwortSenden.GrundURL = "URL";
        AntwortSenden.ErweiterungsURL = "540697";
        AntwortSenden.Methode = "Patch";
        AntwortSenden.DateSenden = Test;
        AntwortSenden.WebserviceSenden();
     }

Error message

Many thanks in advance

MS4IM
  • 3
  • 4
  • Most of your comments add no clarification to the code, and actually hinder readability. –  Feb 02 '17 at 20:46
  • AntwortSenden.Methode = "POST"; Also you may replace the Test variable initialization by "Test = new JavaScriptSerializer().Serialize(KorrekturLagereinheit)". – Graffito Feb 02 '17 at 21:31

0 Answers0