我有一个看起来像这样的代码,它基本上是一个JSON请求,我需要传递给服务器,PHP中的代码如下所示:
<?php
$client_secret= '';
$data= array(
'email' => '**********',
'password' => '******',
'client_id' => '*******'
);
$api_url='******';
$json_data=json_encode($data);
$signature_string = md5($json_data . $client_secret);
$post_data = 'signature='.$signature_string.'&data='.urlencode($json_data);
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
?>
这在PHP中运行良好。所以现在我正在尝试将此代码转换为C#。
到目前为止我所做的是:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
string client_secret = "*****";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"email\":\"******\"," +
"\"password\":\"******\"}" +
"\"client_id\":\"******\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
string resp = string.Empty;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
resp = streamReader.ReadToEnd();
}
return resp;
基本上我现在拥有JSON格式的数据。现在我需要做的是复制PHP的md5函数,从我在上一步创建的client_secret字符串+ JSON字符串中创建一个MD5哈希,然后将数据字符串发布到服务器。
有人可以帮我解决这个问题吗?