Tag: rest

C#REST客户端使用POST发送数据

我正在尝试向REST Web服务发送一个简单的POST请求并打印响应(代码如下,主要来自Yahoo!开发人员文档和一些文档提供的MSDN代码片段)。 我希望客户发送: 请求方法:POST(即我希望PHP中的$ _SERVER [‘REQUEST_METHOD’] ==’POST’) 数据:foo = bar(即PHP中的$ _POST [‘foo’] ==’bar’) 但是,它似乎在发送: 请求方法:FOO = BARPOST 数据:(空白) 我知道API是有效的,因为我已经用Python和PHP编写的客户端进行了测试,所以我很确定它一定是我的C#的问题。 我不是一个交易的.NET程序员,所以会很感激任何关于如何弄清楚问题是什么的评论/指示 – 我确信它是微不足道的但是我自己无法发现它。 uri,用户和密码变量在代码中设置得更早 – 它们可以正常使用GET请求。 request = (HttpWebRequest) WebRequest.Create(uri); request.Credentials = new NetworkCredential(user, password); request.Method = WebRequestMethods.Http.Post; request.ContentType = “application/x-www-form-urlencoded”; string postData = “foo=bar”; request.ContentLength = postData.Length; StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII); postStream.Write(postData); postStream.Close(); […]

具有可空类型的REST?

我打了砖墙。 我的REST实现不接受Nullable值。 [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = “/Transactions?AccNo={AccNo}&CostCentreNo={CostCentreNo}&TransactionType={TransactionType}&Outstanding={Outstanding}&CheckStartDate={CheckStartDate}&CheckEndDate={CheckEndDate}”)] List GetTransactions(Int32 AccNo, Int32 CostCentreNo, Int32 TransactionType, Boolean Outstanding, DateTime? CheckStartDate, DateTime? CheckEndDate); 而我原来的SOAP实现确实如此。 那么有办法解决这个问题吗? 或者我是否必须重新编写代码? 我仍然不明白为什么日期时间必须可以为空而无法设置为null。

我可以在RESTful服务中使用TCP吗?

REST正在使用Web的当前function并在其上应用一些原则以提高其效率。 它使用标准HTTP动词进行通信,并利用其无状态特性。 但是,REST服务是否可以使用TCP协议进行通信? 如果是,那么它会违反其原则吗?

WCF REST文件上载

我正在开发一个WCF Web服务,需要能够上传文件等。 目前我添加’floorplan’项的方法如下: [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “Floorplan?token={token}&floorplan={floorplan}”)] string XmlInputFloorplan(string token, string floorplan); 我需要更改它,以便将图像作为此调用的一部分上载,可以在以下方法中使用: public static Guid AddFile(byte[] stream, string type); 在这种情况下, byte[]是图像的内容。 然后将得到的guid传递给数据层,并最终确定平面图的添加。 所以我需要弄清楚两件事: 1)我应该如何改变XmlInputFloorplan接口方法,以便它还允许图像作为参数? 2)如何在更改后使用服务? 谢谢! 这是我解决它的方式: [OperationContract] [WebInvoke(Method = “POST”, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “Floorplan”)] XmlDocument XmlInputFloorplan(Stream content); 期待输入XML,如: Image包含一个base […]

使用REST和C#实现Google音译API,面临unicode和解析问题

我一直在尝试使用RESTful方法使用Google Transliterate API,因为它很容易通过服务器端语言(C#here)来实现。 因此,我遇到了以下url格式: http : //www.google.com/transliterate/indic? telqt = 1&lapapair = en | hi&text = bharat %2Cindia&tl_app = 3 ,它以下列格式返回JSON: [ { “ew” : “bharat”, “hws” : [ “भारत”,”भरत”,”भरात”,”भारात”,”बहरत”, ] }, { “ew” : “india”, “hws” : [ “इंडिया”,”इन्डिया”,”इण्डिया”,”ईन्डिया”,”इनडिया”, ] }, ] 我尝试了HttpWebRequest和HttpWebResponse来获取JSON,但它在Web浏览器上以Unicode的forms返回值,例如: [ { “ew” : “bharat”, “hws” : [ “\u092D\u093E\u0930\u0924″,”\u092D\u0930\u0924″,”\u092D\u0930\u093E\u0924″,”\u092D\u093E\u0930\u093E\u0924″,”\u092C\u0939\u0930\u0924”, ] }, { […]

如何将参数传递给WCF post方法(一个restful服务)

我正在开发基于WCFrest的服务。 我在我的服务中编写了Get和Post方法,并且当我输入URL(JSON格式)时,Get方法能够工作(获取数据)。 问题是,当我尝试对POST方法执行相同操作时,该URL正在导航到其他页面“找不到页面…”。 我知道POST方法需要表单提交来处理请求。 出于这个原因,我尝试了chrome扩展(Simple Rest客户端,Advanced Rest客户端,Post man rest客户端)和Fiddler。 这里我发布我的服务方法 – Get方法(接口方法声明)。 [OperationContract] [WebInvoke(Method = “GET”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = “GetCategoryTypes/”)] List GetCategoryTypes(); 这是我的POST方法 [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “AddOrders/”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] int AddOrders(decimal amount, int tableID, DateTime orderDate, int […]

WebRequest不发送客户端证书

我正在为REST API编写客户端并对API进行身份validation我必须使用提供给我的证书。 这段代码如下: public string GetCustomer(int custId) { X509Certificate2 Cert = new X509Certificate2(); Cert.Import(@”C:\users\foo\desktop\api\pubAndPrivateCert.pkcs12″, “”, X509KeyStorageFlags.PersistKeySet); ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(“https://api.foo.net/api/customer/v1/” + custId); req.ClientCertificates.Add(Cert); req.UserAgent = “LOL API Client”; req.Accept = “application/json”; req.Method = WebRequestMethods.Http.Get; string result = null; using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { StreamReader reader = new StreamReader(resp.GetResponseStream()); result = reader.ReadToEnd(); […]

如何使用REST API将附件发布到JIRA?

如何在C#中使用JIRA REST API和HttpWebRequest将附件发布到JIRA? 从/ rest / api / 2 / issue / {issueIdOrKey} / attachments下的文档 : POST 为问题添加一个或多个附件。 此资源需要一个多部分post。 媒体类型的multipart / form-data在RFC 1867中定义。大多数客户端库都有类使得处理多部分post变得简单。 例如,在Java中,Apache HTTP Components库提供了一个MultiPartEntity,使得提交多部分POST变得简单。 为了防止XSRF攻击,因为此方法接受multipart / form-data,所以它具有XSRF保护。 这意味着您必须提交X-Atlassian-Token标头:nocheck请求,否则将被阻止。 包含附件的multipart / form-data参数的名称必须是“file” 上传名为“myfile.txt”的文件以发布REST-123的简单示例: curl -D- -u admin:admin -X POST -H“X-Atlassian-Token:nocheck”-F“file=@myfile.txt” http://myhost.test/rest/api/2/issue/TEST -123 /附件 我有 foreach (JIRAAttachments attachm in attachments.attachments) { request = HttpWebRequest.Create( logInformation.GetUri() […]

.NET相当于curl将文件上传到REST API?

我需要将一个ics文件上传到REST API。 给出的唯一示例是curl命令。 用于使用curl上传文件的命令如下所示: curl –user {username}:{password} –upload-file /tmp/myappointments.ics http://localhost:7070/home/john.doe/calendar?fmt=ics 如何在C#中使用HttpWebRequest执行此操作? 另请注意,我可能只将ics作为字符串(而不是实际文件)。

Amazon S3 REST API 403错误c#

我正在尝试使用REST API通过C#获取一些代码来从S3获取文件。 我见过其他人做类似的事情,但由于某种原因我不断收到403错误。 我已经尝试使用适用于.Net的AWS SDK做同样的事情并且它可以工作,所以我认为这是我创建授权标头的方式。 有人能够对此有所了解吗? string awsAccessId = “***”; string awsSecretKey = “***”; string bucketName = “thebucket”; string httpDate = DateTime.Now.ToString(“ddd, dd MMM yyyy HH:mm:ss +0000\n”); string canonicalString = “GET\n” + “\n” + “\n” + “x-amz-date:” + httpDate + “\n” + “/” + bucketName + “/readme.txt”; // now encode the canonical string Encoding ae […]