带有 GET 方法的 C# HTTP 正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12013204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# HTTP Body with GET method
提问by Fernando
I'm using an API that requires me to set the method to GET and include a message body. However when I try to do this I get the following error: "Cannot send a content-body with this verb-type". I read that the HttpWebRequest class does not support this and is the reason for the exception. Is there a work around?
我使用的 API 要求我将方法设置为 GET 并包含消息正文。但是,当我尝试这样做时,我收到以下错误:“无法发送具有此动词类型的内容正文”。我读到 HttpWebRequest 类不支持这一点,这是异常的原因。有解决办法吗?
This is my current code: data is a json string encoded as a byte array
这是我当前的代码:数据是编码为字节数组的 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);
}
This is the PHP code I'm trying to emulate
这是我试图模仿的 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);
?>
Thank you,
谢谢,
采纳答案by Jon Hanna
What does one call an API that actively goes against REST? "HASTE"? "DISQUIET"?
什么叫积极反对 REST 的 API?“急”?“忧虑”?
With a bit of luck they service just doesn't care what the verb is and the PHP code was just happening to use GET and hit the bug that the server didn't block it which is a pretty minor bug as long as it behaves correctly, and it'll be fine with POST.
幸运的是,他们的服务并不关心动词是什么,而 PHP 代码恰好使用 GET 并遇到了服务器没有阻止它的错误,只要它行为正确,这是一个非常小的错误, POST 就可以了。
Failing that, your best bet is to see if they have an alternative method that either (if it's a reading request that naturally fits GET) accepts parameters in the URI with perhaps appropriate headers being used as per RFC 2616, or else can accept something through POST, GET etc.
如果失败,最好的办法是查看他们是否有替代方法(如果它是自然适合 GET 的读取请求)接受 URI 中的参数,其中可能根据 RFC 2616 使用适当的标头,或者可以通过以下方式接受某些内容POST、GET 等
If that doesn't work, you'll have to build an HTTP client on top of TcpClient. Which would be pretty horrible.
如果这不起作用,您必须在 TcpClient 之上构建一个 HTTP 客户端。这将是非常可怕的。
回答by Dennis Traub
It is not recommended to send content with a GET request. See this post for further details: HTTP GET with request body
不建议使用 GET 请求发送内容。有关更多详细信息,请参阅此帖子:HTTP GET with request body
回答by Ian Kemp
It is entirely possible, but you have to use the newer HttpClient class: https://stackoverflow.com/a/47902348/70345
完全有可能,但您必须使用较新的 HttpClient 类:https: //stackoverflow.com/a/47902348/70345

