简体   繁体   中英

C# POST request works on POSTMAN but not on C# HttpWebRequest

I'm getting code 400 only in C# and when I use post man I get 200! It has the same properties.

I initally created the classes as JSONProperty attributes and still after decentralize I get code 400.

Found on fiddler innder exception - message=parameters : The property 'Changedby' does not exist on type 'XXXXX.AzureAd.XXX.Types.NewDescriptionEntry'. Make sure to only use property names that are defined by the type.

在此处输入图片说明 The JSON in the debugging mode in C# after creating the classes - JSON mode.

在此处输入图片说明

 // Serialize our concrete class into a JSON String
                var stringPayload = await Task.Run(() => 

JsonConvert.SerializeObject(payload_transferIncident));

            // build the URL we'll hit
            var url = string.Format("https://XXXXXX", "YYYYY", id, "XXXX");

            //create the request
           var req = HttpWebRequest.CreateHttp(url);

            //add in the cert we'll authenticate with
             req.ClientCertificates.Add(IcmIncidentOperation.GetCert("XXXXXX"));

            req.ContentType = "application/json";
            req.Method = "POST";
            if (req == null)
                throw new ApplicationException(string.Format("Could not create the httprequest from the url:{0}", url));
            try
            {
                using (var streamWriter =   new StreamWriter(req.GetRequestStream()))
                {
                    streamWriter.Write(stringPayload);
                }
                var httpResponse = (HttpWebResponse)req.GetResponse();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                var httpResponse = (HttpWebResponse)req.GetResponse();
            }

Try this way using HttpRequest POST METHOD

try
        {
            var baseAddress = new Uri("Your base url");

            using (var httpClient = new HttpClient
            {
                BaseAddress = baseAddress
            })
            {
                var json =Newtonsoft.Json.JsonConvert.SerializeObject( payload);

                using (var content = new StringContent( json, System.Text.Encoding.Default, "application/json"))
                {
                    try
                    {
                        using (var response = await httpClient.PostAsync("end-point url method", content))
                        {

                            result = await response.Content.ReadAsStringAsync();
                        }
                    }
                    catch(Exception ex)
                    {
                        return ex.Message;
                    }

                }
            }

        }
        catch (Exception ex)
        {
            result = ex.Message;
        }

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