How to make an HTTP request using a custom verb?

◇◆丶佛笑我妖孽 提交于 2021-02-04 16:39:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!