-1

I am trying to call a API in my Windows App .Net 4.6.1

HttpClient client = new HttpClient();
            Uri baseAddress = new Uri("https://smsmisr.com/");
            client.BaseAddress = baseAddress;
            var sendtime = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");

            HttpResponseMessage response = client.PostAsJsonAsync(
            "api/webapi/?" +
            "username=XXXxx" +
            "&password=XXXXXX" +
            "&language= 3 Or 2 Or 1" +
            "&sender=Your Sender " +
            "&mobile=2012XXXXXX, 2011XXXX" +
            "&message=Encoded Message" +
            "&DelayUntil=" + sendtime
            ).Result;

and while writing the code I am getting the error

Error CS1501 No overload for method 'PostAsJsonAsync' takes 1 arguments

after searching i added this references :

using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting.dll;

but this error appear: Error CS0234 The type or namespace name 'dll' does not exist in the namespace 'System.Net.Http.Formatting' (are you missing an assembly reference?)

2 Answers2

1

The error is pretty clear. Here you can see what all the overloads of that method are. You probably want to pass the uri and the payload object, something like:

HttpResponseMessage response = client.PostAsJsonAsync("/api/webapi", [your object instance here]);

Currently, you are passing a single String, hence the error.

Also, the using statement that needs correction:

using System.Net.Http.Formatting;
  • You may as well add in the answer to the other error he is getting (he has `using System.Net.Http.Formatting.dll`, but it should be `using System.Net.Http.Formatting`) – Andy Aug 03 '20 at 03:50
  • i tied that and the response msg is : {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Mon, 03 Aug 2020 08:00:39 GMT Server: Microsoft-IIS/10.0 Content-Length: 75 Content-Type: text/html }} – Bassem Kamal M Aug 03 '20 at 08:02
  • That means you are sending data to the server the server probably doesn't know how to interoperate and is crashing. – Andy Aug 03 '20 at 13:59
-1

Did you implemented PostAsJsonAsync method that takes one argument?

That error occurs when user did not implement a method required by inherited class

GoodSSen
  • 7
  • 1
  • That error shows up when it's implemented -- he just isn't sending in the correct amount of parameters to said method. This answer is wrong. – Andy Aug 03 '20 at 02:17