1

I am using Visual Studio 2022 to develop a Winforms program, and Visual Studio reported a http 400 error when calling Web API.

Here is the code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request. Referer = Referer;

byte[] bytes = Encoding.UTF8.GetBytes(Data);

request.ContentType = "application/json";
request.ContentLength = bytes.Length;

Stream myResponseStream = request.GetRequestStream();

myResponseStream.Write(bytes, 0, bytes.Length);

HttpWebResponse response = (HttpWebResponse) request.GetResponse(); //error

Error:

System.Net.WebException: "Theremoteserver returned an error: (400) Bad Request.

This is my Web API controller:

 [Route("api/MyWebApi")]
 [ApiController]
 public class MyWebApiController : ControllerBase
 {
     private readonly ILogger<MyWebApiController> _logger;

     public MyWebApiController(ILogger<MyWebApiController> logger)
     {
         _logger = logger;
     }

     [HttpPost("[action]")]
     public IActionResult SayHello([FromBody] UserModel model)
     {
         if (model.pwd != "1")
         {
             object obj = new
             {
                 code = "0",
                 msg = "Request failed"
             };

             return new JsonResult(obj);
             //return new BadRequestResult();
         }
         else
         {
             object obj = new
             {
                 code = "1",
                 msg = "Request successful"
             };

             return new JsonResult(obj);
         }
     }
 }

I tried this ASP.NET Web Api HttpResponseException 400 (Bad Request) Hijacked by IIS and it doesn't seem to work. Can someone help me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • How is your data look like ? Your sample code does not contain data that you are posting. – dotnetstep Apr 16 '23 at 14:44
  • The data is determined as a string type parameter in the post static method, I don't think there is anything special about it – Bilin Vance Apr 16 '23 at 16:44
  • 1
    @BilinVance Can you tell me in detail what is the data you passed in? An example can be given to illustrate. The issue is likely to be there – sssr Apr 17 '23 at 02:27
  • Do you mean the transmitted data is wrong? – Bilin Vance Apr 17 '23 at 11:30
  • @BilinVance Yes, because I noticed that your ContentType is application/json. Then you cannot use string or int when passing data, you need to maintain the consistency of the data type. You can refer to [How to Turn a C# Object Into a JSON String in .NET?](https://code-maze.com/csharp-object-into-json-string-dotnet/) – sssr Apr 18 '23 at 01:50
  • Thanks for your reply, I have solved it. As you said, there is a problem with the type of the transmitted data. I have already processed the json – Bilin Vance Apr 18 '23 at 15:07

1 Answers1

0

Because I noticed that your ContentType is application/json. Then you cannot use string or int when passing data, you need to maintain the consistency of the data type. You can refer to How to Turn a C# Object Into a JSON String in .NET?

sssr
  • 364
  • 1
  • 6