Erik
Erik

Reputation: 954

HttpClient: how to send parameters and retreive answer

I need to send a request to a license server. I tried it using FireFox like this:

http://my.server.com/sub/?aaa=5d1606&bbb=ccc&key=5d160

and that works. I get the correct response. Now I do it in C#:

using (var client = new HttpClient())
{
    var SKey = "blabla";
    var LKey = "bloblo";
    string param = String.Format($"?aaa={SKey}&bbb=ccc&key={LKey}");
    Debug.WriteLine(param);
    var content = new StringContent(param);
    HttpResponseMessage response = await client.PostAsync("http://my.server.com/sub/", content);
    responseString = await response.Content.ReadAsStringAsync();
    Debug.WriteLine(responseString);
}

That don't work: the responseString is null. Any suggestions/solutions/remarks? I'd be grateful.

Upvotes: 0

Views: 60

Answers (1)

Garrett Manley
Garrett Manley

Reputation: 337

Your content is not being serialized to be passed in the POST request like you are doing.

Either setup your server to recieve a JSON body content and use a JSON seralizer to pass the content to the service, or append the paramters to the url and just pass that to your request.

HttpResponseMessage response = await client.PostAsync("http://my.server.com/sub/" + param);

Upvotes: 1

Related Questions