我正在使用一个API,需要我将方法设置为GET并包括一个消息体。但是当我尝试这样做时,我收到以下错误消息:“Cannot send a content-body with this verb-type”。我了解到HttpWebRequest类不支持此操作,并且这是异常的原因。是否有解决方法?
以下是我的当前代码:数据是作为字节数组编码的JSON字符串。
谢谢您,
以下是我的当前代码:数据是作为字节数组编码的JSON字符串。
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(data.ToArray(), 0, (int)data.Length);
}
这是我试图仿效的PHP代码
<?php
$data = array("id" => "1234");
$data_string = json_encode($data);
$ch = curl_init('url');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
var_dump($result);
?>
谢谢您,