Tag: rest

使用多个参数WITHOUT DataContract来使用WCF REST服务

我需要使用POST方法使用多个参数调用我的WCF REST服务,但是我无法创建包含我的参数的DataContract,因为我需要简单的类型:我的webservice将由目标C应用程序使用。 我在MSDN网站上找到了这种语法: [OperationContract] [WebInvoke(Method = “POST”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “savejson?id={id}&fichier={fichier}”)] bool SaveJSONData(string id, string fichier); 为了快速解释上下文,我必须调用此方法来保存带有在数据库上传递的Id的JSON文件。 我的问题是:是否真的可以将几个参数传递给POST方法,如前所示? 其次:如何使用多个参数来使用我的服务(暂时使用C#,只是为了测试它)? 我已经使用DataContract进行了测试,我就是这样做的: string url = “http://localhost:62240/iECVService.svc/savejson”; WebClient webClient = new WebClient(); webClient.Headers[“Content-type”] = “application/json; charset=utf-8”; RequestData reqData = new RequestData { IdFichier = “15”, Fichier = System.IO.File.ReadAllText(@”C:\Dev\iECV\iECVMvcApplication\Content\fichier.json”) }; MemoryStream […]

WebAPI 2属性路由,区域不起作用

我无法使WEBAPI 2属性路由工作。 我正在寻找的路由方案是/api/{product}/{controller}/{id-optional} ,所以就像/api/Vision/IdCard 。 控制器位于一个区域中,并设置如下: namespace DataServices.Controllers.Vision { [RoutePrefix(“api/vision”)] public class IdCardController : BaseApiController { [System.Web.Mvc.RequireHttps] [Route(“idcard”)] public IdCardViewModel Get(HttpRequestMessage request) {…} 每当我做到这一点,我得到一个404 。 我包含了命名空间,因为该区域位于其自己的命名空间中。 WEBAPI属性路由中是否支持区域? 编辑:WebApiConfig看起来像这样: config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional } );

使用POST请求将JSON发送到Jira API时的System.Net.WebException

好吧,伙计们,我已经在这个问题上挣扎了一天左右,没有明确的解决方案。 我将从例外开始: The remote server returned an error: NotFound. at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 我正在尝试连接到JIRA rest API以登录用户。 我们的JIRA系统目前运行4.4.1,我试图访问的API信息在此处记录: https : //developer.atlassian.com/static/rest/jira/4.4.1.html 。 (请参阅“/ auth / 1 / session”API的POST请求) API调用POST请求,其中包含用户凭据的JSON正文。 我已尝试手动构建JSON,以及使用JSON库,结果是相同的。 我发送的json是: { “username”: “test”, “password”: “test” } 我试图将内容类型和编码更改为我能想象的任何内容。 这包括“text / json”,“application / json”,将Encoding.UTF8添加到流编写器等。所有结果都是相同的。 也许这整个考验中最令人沮丧的部分是,我能够立刻用Java for Android编写这个,所以我不认为这是一个API误解,就像Windows Phone 8和/或C#一样误解。 最后要指出的几点: 如果我更改代码以使用GET请求,请指向“http://www.google.com”,并删除请求回调(直接跳到响应),一切正常,我得到了我期望的结果。 我对HttpWebRequest的“BeginX”“EndX”方法感到困惑。 我理解异步任务,但不完全是C#如何管理它。 […]

使用C#从REST中的REST Api检索数据而不会出现滞后或抖动

我每隔5秒使用C#脚本调用REST。 但问题是应用程序每10秒钟会停滞一次(InvokeRepeating)并等待服务器响应,然后在滞后后给出结果。 如何克服这个问题? 我需要实时访问特定的行,以便检索更改而不会出现任何延迟。 以下代码片段用于读取REST Api using UnityEngine; using System.Collections; using System.Net; using System.IO; using System.Collections.Generic; using System.Linq; using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public class Rows { public string description ; public float Efficiency; public bool IsConnected; public string Lastconnection; public float MTBF; public float MTTR; public string Name; public float Speed; public float […]

RestSharp JsonDeserializer,标识符中包含特殊字符

我有一些来自Last.fm的JSON,如下所示: { “toptags”:{ “@attr”:{ “artist”:”Whatever”, “album”:”Whatever” } } } 是否有一种特殊的方法来设置RestSharp以使其识别@attr ? @(AT符号)导致我出现问题,因为我无法创建与此匹配的标识符。

REST服务上的错误请求错误使用POST(json数据)调用方法?

嗨,我是RESTful WCF的新手,我正在尝试使用POST对webservice方法进行简单的调用,这是我的代码 Service Interface code [ServiceContract] public interface IJsonSave { [OperationContract] [WebInvoke(UriTemplate = “/SaveJason”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = “POST”, BodyStyle = WebMessageBodyStyle.WrappedRequest)] string SaveJason(string value); } web.config文件代码 我尝试使用提琴手她是我的标题 POST http://localhost:50267/JsonSave.svc/Rest/SaveJason User-Agent: Fiddler Content-Type: application/json Data-Type: json Host: localhost:50267 申请机构: ({“value”:”asdfasd”}) 它给出了HTTP/1.1 400 Bad Request错误,当启用调试细节时,它会给出以下堆栈跟踪 The server encountered an error processing the […]

C#REST API调用 – 在邮递员中工作,而不是在代码中工作

我有一些现有的代码正在工作,并且突然退出。 我无法弄清楚为什么…… 这是我的代码: public static string RequestToken(string u, string pw) { string result = string.Empty; string strUrl = “https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=” + u + “&password=” + pw; HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest; Debug.Print(strUrl); tokenRequest.Method = “POST”; try { using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse) { if (tokenResponse.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format( “Server error (HTTP […]

Web API在运行时对属性进行条件序列化

我正在寻找在ASP.Net中使用WebAPI构建API。 我要求在RunTime而不是Compile Time根据某些自定义逻辑有条件地从XML或JSON中排除属性。 我必须从响应中删除xml或json,仅包含null或空值的标记并不好。 我尝试了各种方法,但我似乎无法开始工作。 我尝试了以下内容 从这里 委派处理程序 public class ResponseDataFilterHandler : DelegatingHandler { protected override System.Threading.Tasks.Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return base.SendAsync(request, cancellationToken) .ContinueWith(task => { var response = task.Result; //Manipulate content here var content = response.Content as ObjectContent; if (content != null && content.Value != null) { } //Or replace the content […]

最佳实践以及如何在客户端的C#包装器中支持不同版本的REST API

我编写了一个C#包装器来支持我们公司项目的REST API。 但是,这些API现在正在发生变化 – 就URL而言(可能会在URL中引入版本号)和它期望的数据对象并返回。 我想知道在我的c#wrapper中支持不同版本的REST API的最佳做法是什么。 我应该怎么做 – 在代码设计和类定义方面 – 以便一个包装器可以与不同版本的API无缝地工作 – 并且它也应该是可扩展的 – 以便将来可以轻松支持任何更新版本的API 。 我编写的c#包装器正在使用我们的Web服务API。 我已经在使用RestSharp客户端在c#wrapper中使用我们的Web服务API。

如何使WCF RESTful服务工作异步?

我正在构建WCFrest服务,它是客户端。 我计划,客户端对服务知之甚少,只是正确的URL来调用方法和预期结果。 我的服务合同是: [WebInvoke(Method=”POST”, UriTemplate=”/tasks”)] [OperationContract] void SubmitTask(Transaction task); [WebGet(UriTemplate = “/tasks/{taskId}”)] [OperationContract] [XmlSerializerFormat] Transaction GetTask(string taskId); SubmitTask实现如下: SubmitTask(Transaction task) { DoSomethingWithTask(task); task.Status = “SomeStatus”; DoSomethingElseWithTaks(task); task.Status = “SomeOtherStatus”; } 我对客户的期望: ButtonClick() { SubmitTask(task); while(true) { string status = Transaction GetTask(task.taskId).Status; Textbox.Text+= status; if(status==ok) break; Thread.Sleep(1000); } } 问题是 – GetTask不在服务端执行,而所有SubmitTask操作都已完成,因此我只获得客户端的最后一个任务状态。 如何在这种情况下实现异步操作? 提前致谢!