简体   繁体   English

如何使用自定义动词发出 HTTP 请求?

[英]How to make an HTTP request using a custom verb?

In order to test an API, I want to be able to make HTTP requests using custom verbs (such as "RECOMPUTE")¹, other than GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE and CONNECT.为了测试 API,我希望能够使用自定义动词(例如“RECOMPUTE”)¹ 发出 HTTP 请求,而不是 GET、POST、PUT、DELETE、OPTIONS、HEAD、TRACE 和 CONNECT。

Is there a library which already does that, or do I have to reinvent the wheel using HttpWebRequest and HttpWebResponse ?是否有一个库已经这样做了,还是我必须使用HttpWebRequestHttpWebResponse重新发明轮子?

Answers to another, less specific question , suggest several libraries, none being compatible with what I need:另一个不太具体的问题的答案建议了几个库,但没有一个与我需要的兼容:

  • WebClient doesn't support custom verbs, WebClient 不支持自定义动词,

  • EasyHttp is limited to five verbs, EasyHttp 限制为五个动词,

  • HttpClient seems not maintained (version 0.6.0, released two years ago). HttpClient 似乎没有维护(版本 0.6.0,两年前发布)。


¹ In anticipation of comments such as "Using non-standard verbs is a bad idea", there is nothing I can do with that. ¹ 在预料到诸如“使用非标准动词是个坏主意”之类的评论时,我对此无能为力。 I'm using a private API where the choice was made to use custom verbs.我正在使用私有 API,其中选择使用自定义动词。

Take a look at the System.Net.Http.HttpClient class in .NET 4.5.查看 .NET 4.5 中的System.Net.Http.HttpClient类。 Not only does it support the common ones, but it allows you to specify your own method as well.它不仅支持常见的方法,而且还允许您指定自己的方法。

Construct your own verb by using the HttpMethod class.使用HttpMethod类构造您自己的动词。

var method = new HttpMethod("RECOMPUTE");

Docs:文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM