HttpRequest - POST request, response in C#

Markus Freitag 3,791 Reputation points
2021-05-12T12:54:27.067+00:00

Hello,

I need to port a C++ application to C#
C++ code is below.
How do I get this into C#

Request, response

How can I create classes objects?
How can I analyze the answer easily?
Which class/assembly offers itself there in C#?

Json::Value Food::SendScanResults(std::vector<std::string> serials, std::string date)
{
    HttpRequest::parameters params;
    params.port           = atoi(m_config.port.c_str());
    params.httpVer        = "HTTP/1.1";
    params.host           = m_config.host;
    params.method         = "POST";
    params.path           = m_config.function_path;
    params.accept_version = m_config.version;

    Json::Value json_body;
    json_body["currentDate"] = date;
    json_body["serialNumber"];

    for(int i = 0; i < serials.size(); i++)
    {
        json_body["serialNumber"][i] = serials[i];
    }

    Json::FastWriter fw;
    std::string query = fw.write(json_body);
    if(query.at(query.length()-1) == '\n')
    {
        query.pop_back();
    }

    params.query = query;



    HttpRequest req(params);
    if(!req.send())
    {
        Log("http request failed.");
    }
    std::string response (req.getAnswer());

    Log("Response: " + CString(response.c_str()));

    Json::Value j_response;
    Json::Reader j_reader;
    if(!j_reader.parse(response, j_response))
    {
        j_response = Json::nullValue;
        WriteLog("Error response...");
    }

    if(response.empty())
    {
        j_response["foodData"]["code"] = -1;
        j_response["foodData"]["message"] = response;
        j_response["data"] = "";
    }

    return j_response;
}

Many thanks for your help.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-05-13T05:18:41.85+00:00

    Hi MarkusFreitag-0088,
    I found some similar threads and hope helpful for you.
    How to post JSON to a server using C#?
    In this thread, some codes using httpWebRequest and HttpClient are provided for you to refer to.
    Send JSON via POST in C# and Receive the JSON returned?
    How to make an HTTP POST web request
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-05-12T13:38:28.48+00:00

    The defacto in .NET is to use HTTPClient. There are plenty of examles using HTTLClient doing a post/response action with Jason data out on Bing and Google.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.