问题
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.
Is there a library which already does that, or do I have to reinvent the wheel using HttpWebRequest
and HttpWebResponse
?
Answers to another, less specific question, suggest several libraries, none being compatible with what I need:
WebClient doesn't support custom verbs,
EasyHttp is limited to five verbs,
HttpClient seems not maintained (version 0.6.0, released two years ago).
¹ 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.
回答1:
Take a look at the System.Net.Http.HttpClient
class in .NET 4.5. 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.
var method = new HttpMethod("RECOMPUTE");
Docs:
- HttpClient
- SendAsync
- HttpRequestMessage
- HttpMethod
来源:https://stackoverflow.com/questions/20192164/how-to-make-an-http-request-using-a-custom-verb