将Curl帖子转换为C#

问题描述 投票:-2回答:2

我检查了关于SO的几篇文章,但由于我有一个非常具体的案例而无法使其正常工作。

我有一个使用curl制作的帖子,但我需要使用c#进行完全相同的操作。

这是卷曲的部分,我不知道如何在c#中执行:

$parameters = array(
    'session' => $sessionId,
    'module' => 'Contacts',
    'name_value_list' => array(
        array('name' => 'first_name', 'value' => 'John'),
        array('name' => 'last_name', 'value' => 'Doe'),
        ),
    );

$json = json_encode($parameters);

$post = array(
    'method' => 'set_entry',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => $json
);

curl_setopt($curl, CURLOPT_POSTFIELDS, $post);

// Make the REST call, returning the result
$response = curl_exec($curl);

在C#中,我已经有了初始连接,但是现在我不知道该如何处理帖子本身:

using (var httpClient = new HttpClient())
        {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "xxxxx/rest.php"))
            {
                request.Headers.TryAddWithoutValidation("Accept", "application/json");

                //Post the data.....
            }
        }
c# curl php-curl
2个回答
0
投票

httpClient.SendAsync(request);已经做到了。


0
投票

对不起,没有得到。 curl和c#代码完全不同。

© www.soinside.com 2019 - 2024. All rights reserved.