1

C# code below (they say this code works) produces HTTP request that passes parameters in Body. Web server I'm working with doesn't see these params - but it works when POST request contains params in URL like this:

http://my.zadarma.com/auth/?p=%2Fconnect%2Fsms%2F

The C# code below creates following request putting params in the body:

C#:
POST http://my.zadarma.com/auth/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: my.zadarma.com
Content-Length: 21
Expect: 100-continue
Connection: Keep-Alive

p=%2Fconnect%2Fsms%2F

Postman creates following request - putting params in URL(my server prefer this):

POST http://my.zadarma.com/auth/?p=%2Fconnect%2Fsms%2F HTTP/1.1
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Postman-Token: d6b3df55-d42f-45e8-82b8-a3fd11b02e35
Host: my.zadarma.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 0

How to change following code (using FormUrlEncodedContent or similar) to connect the query string params to URL in POST request? (like Postman does):

public static async Task<HttpStatusCode> UpdateProductAsync()
{
    var url = "http://my.zadarma.com/auth/";

    var client = new System.Net.Http.HttpClient();

    var data = new Dictionary<string, string>
    {
        {"p", "/connect/sms/"}
    };

    var res = await client.PostAsync(url, new FormUrlEncodedContent(data));

    var content = await res.Content.ReadAsStringAsync();
    Console.WriteLine(content);

    var responseString = await res.Content.ReadAsStringAsync();
    return res.StatusCode;

}

I can concatenate URL and params - it will work but I try to avoid concatenation. Updated: Following works. But is there more consistent way to handle params?

        public static async Task<HttpStatusCode> UpdateProductAsync(NetworkParameters networkParameters)
        {
            var url = "http://127.0.0.1:5001/api/v0/files/stat";

            var client = new System.Net.Http.HttpClient();

            string queryStringKey = HttpUtility.UrlEncode("arg", Encoding.UTF8);
            string queryStringValue = HttpUtility.UrlEncode("/KeySet", Encoding.UTF8);

            var res = await client.PostAsync(url + "?" + queryStringKey + "=" + queryStringValue, null);

            var content = await res.Content.ReadAsStringAsync();

            var responseString = await res.Content.ReadAsStringAsync();
            return res.StatusCode;
        }
3
  • Content = request body. You want to build it into the URL. Commented Oct 6, 2022 at 13:56
  • @ProgrammingLlama Could you please elaborate? I rarely use HTTP. Commented Oct 6, 2022 at 13:58
  • @ProgrammingLlama I see that FormUrlEncodedContent returns HttpContent content - does this mean that this function always will construct content (= body), that is place params in body (never in URL)? Commented Oct 6, 2022 at 14:14

2 Answers 2

1

You can use the queryhelper class from Microsoft.AspNetCore.WebUtilities.dll
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webutilities.queryhelpers.addquerystring?view=aspnetcore-7.0

var newUrl = QueryHelpers.AddQueryString(url, data);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - it is intended exactly for this purpose. Do you know is it good idea to use .Net Framework together with ASP Net Core? This is small client application and I'm thinking whether it's worth to introduce such dependency.
0

@Vlad You can not use .Net Framework with Net Core but you can create a .NET Standard 2.0 project and use this for both .Net Framework and .Net Core

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.