Tag: rest

在C#中解析Jsonrestapi响应

我试图使用C#从rest api json响应中提取值。 我有以下代码: client.BaseUrl = “https://api.cloud.appcelerator.com”; request.Resource = “/v1/chats/create.json?key=” + cac.AppCode.ToString(); request.Method = Method.POST; request.AddUrlSegment(“appkey”, “key”); var response = client.Execute(request); 在“响应”消息中,我得到了一个json内容如下: { “meta”: { “code”: 200, “status”: “ok”, “method_name”: “createChatMessage” }, “response”: { “chats”: [ { “id”: “521cfcd840926a0b3500449e”, “created_at”: “2013-08-27T19:24:08+0000”, “updated_at”: “2013-08-27T19:24:08+0000”, “message”: ” join to the chat group, welcome …”, “from”: { “id”: […]

如何在.NET中使用Restful服务?

使用.Net框架使用RESTful服务有哪些选择? 什么时候WCF(使用WebChannelFactory)比HttpClient更可取?

为什么我的带有双args的Web API方法没有被调用?

我有一个Web API方法,需要两个双参数: 存储库界面: public interface IInventoryItemRepository { . . . IEnumerable GetDepartmentRange(double deptBegin, double deptEnd); . . . } 库: public IEnumerable GetDepartmentRange(double deptBegin, double deptEnd) { // Break the doubles into their component parts: int deptStartWhole = (int)Math.Truncate(deptBegin); int startFraction = (int)((deptBegin – deptStartWhole) * 100); int deptEndWhole = (int)Math.Truncate(deptEnd); int endFraction = (int)((deptBegin […]

为什么我的C#客户端,POST到我的WCF REST服务,返回(400)错误请求?

我正在尝试向我写的一个简单的WCF服务发送POST请求,但我一直收到400 Bad Request。 我正在尝试将JSON数据发送到服务。 谁能发现我做错了什么? 🙂 这是我的服务界面: public interface Itestservice { [OperationContract] [WebInvoke( Method = “POST”, UriTemplate = “/create”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] String Create(TestData testData); } 实施: public class testservice: Itestservice { public String Create(TestData testData) { return “Hello, your test data is ” + testData.SomeData; } } DataContract: [DataContract] public class […]

将文件从Html表单(multipart / form-data)上传到WCF REST服务作为流而不流式传输整个表单的输入?

我在将文件从Html上传到我的rest服务(WCF REST)时遇到了问题。 在上传文件时,我想发送标题和说明等信息以及文件的内容。 所以,我创建了一个这样的测试表单: Title: Description: Filename: File: 服务器端,我想将其翻译为此方法: [OperationContract] [WebInvoke( BodyStyle = WebMessageBodyStyle.Bare, Method = “POST”, UriTemplate = “/Note/{noteId}/Attachment”)] [Description(“Add an attachment to a Note.”)] void AddAttachmentToNote(string noteId, AttachmentRequestDto attachmentRequestDto); 将AttachmentRequestDto定义为 [DataContract] public class AttachmentRequestDto { [DataMember] public string Title { get; set; } [DataMember] public string Description { get; set; } [DataMember] public […]

Web API – 405 – 请求的资源不支持http方法’PUT’

我有一个Web API项目,我无法启用针对它的“PUT / Patch”请求。 我从小提琴手那里得到的回应是: HTTP/1.1 405 Method Not Allowed Cache-Control: no-cache Pragma: no-cache Allow: GET,POST,DELETE Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcZG90TmV0XFdlYkFQSVxBZFNlcnZpY2VcQWRTZXJ2aWNlXGFwaVxpbXByZXNzaW9uXDE1?= X-Powered-By: ASP.NET Date: Tue, 06 May 2014 14:10:35 GMT Content-Length: 72 {“message”:”The requested resource does not support http method ‘PUT’.”} 根据上述回复,不接受“PUT”动词。 但是,我无法弄清楚相关处理程序的配置位置。 类的“Put”方法声明如下: [HttpPatch] [HttpPut] public HttpResponseMessage Put(Int32 […]

授权标头在重定向时丢失

下面是进行身份validation的代码,生成Authorization标头并调用API。 不幸的是,我在API上发出GET请求后收到401 Unauthorized错误。 但是,当我捕获Fiddler中的流量并重放它时,对API的调用成功,我可以看到所需的200 OK状态代码。 [Test] public void RedirectTest() { HttpResponseMessage response; var client = new HttpClient(); using (var authString = new StringContent(@”{username: “”theUser””, password: “”password””}”, Encoding.UTF8, “application/json”)) { response = client.PostAsync(“http://host/api/authenticate”, authString).Result; } string result = response.Content.ReadAsStringAsync().Result; var authorization = JsonConvert.DeserializeObject(result); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Scheme, authorization.Token); client.DefaultRequestHeaders.Add(“Accept”, “application/vnd.host+json;version=1”); response = client.GetAsync(“http://host/api/getSomething”).Result; Assert.True(response.StatusCode == HttpStatusCode.OK); […]