如何使用自定义动词发出HTTP请求? [英] How to make an HTTP request using a custom verb?

查看:66
本文介绍了如何使用自定义动词发出HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了测试API,我希望能够使用GET,POST,PUT,DELETE,OPTIONS,HEAD,TRACE和CONNECT之外的自定义动词(例如"RECOMPUTE")¹发出HTTP请求./p>

是否已经有一个库做到了这一点,还是我必须使用 HttpWebRequest HttpWebResponse 重新发明轮子?

另一个不太具体的问题的答案,建议了几个库,没有一个与我需要的库兼容:

  • WebClient不支持自定义动词,

  • EasyHttp被限制为五个动词,

  • HttpClient似乎没有得到维护(两年前发布的0.6.0版).


¹期待诸如使用非标准动词是一个坏主意"之类的评论,对此我无能为力.我使用的是专用API,可以选择使用自定义动词.

解决方案

看看.NET 4.5中的 System.Net.Http.HttpClient 类.它不仅支持通用方法,还允许您指定自己的方法.

使用 HttpMethod 类构造自己的动词.

  var方法=新的HttpMethod("RECOMPUTE"); 

文档:

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.

解决方案

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:

这篇关于如何使用自定义动词发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆