标签: restsharp

休息不成功的帖子

我正在尝试使用restsharp来使用休息服务(wcf)

这是我的服务

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
        Employee PostGetEmploy(Employee emp);
    }

    [DataContract]
    public class Employee
    {
        [DataMember]
        public int EmpNo { get; set; }
        [DataMember]
        public string EmpName { get; set; }
        [DataMember]
        public string DeptName { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我就是这么称呼它的

    var client = new RestClient("http://localhost:14437/Service.svc");
    var request = new RestRequest("XmlService/PEmploy", Method.POST);
    myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" }; …
Run Code Online (Sandbox Code Playgroud)

c# rest restsharp

4
推荐指数
1
解决办法
4104
查看次数

RestSharp - 从 POSTed 响应中检索授权令牌

我正在尝试将用户名和密码传递到以下 URL:

https://maxcvservices.dnb.com/rest/Authentication
Run Code Online (Sandbox Code Playgroud)

根据文档,user_id 和密码必须作为标头传递,并分别带有键:x-dnb-user、x-dnb-pwd。

到目前为止,我有以下代码似乎可以工作,但我无法检索响应对象返回的身份验证令牌:

public static void Main (string[] args)
{
    var client = new RestClient ("https://maxcvservices.dnb.com/rest/Authentication");
    var request = new RestRequest (Method.POST);
    request.AddHeader("x-dnb-user", myEmail);
    request.AddHeader("x-dnb-pwd", myPassword);
    IRestResponse resp = client.Execute(request);
    var content = resp.Content;
    Console.WriteLine (resp.StatusDescription);
    Console.WriteLine (resp.StatusCode);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试打印内容时,我得到一个空行,但我实际上期望的是服务返回的身份验证令牌。我认为我在代码中做的几件事(但不确定)是将用户 ID 和密码作为 POST 请求中的标头传递,这是所需的。该令牌作为响应对象中“授权”字段的值返回。我想知道如何打印令牌。另外,statusDescription、statusCode 都打印 OK,这告诉我我有正确的请求,但无法在响应中找到身份验证令牌。如果能指导我如何访问返回的 POST 响应的授权字段中的身份验证令牌,我们将不胜感激。

c# authentication http-post restsharp

4
推荐指数
1
解决办法
9357
查看次数

将 URL 查询字符串附加到请求

我正在尝试发送POST请求并以特定格式格式化查询字符串。除了第一个参数之外,顺序并不重要,但我还没有成功。

我需要的:

本地主机/someapp/api/dosomething/5335?save=false&userid=66462

我的一些尝试吐出了什么:

http://localhost/someapp/api/dosomething/?Id=29455&save=false&userId=797979 http://localhost/someapp/api/dosomething/?save=false&userId=797979

我如何格式化请求:

    request.AddQueryParameter("Id", "29455");
    request.AddQueryParameter("save", "false");
    request.AddQueryParameter("user", "4563533245");
Run Code Online (Sandbox Code Playgroud)

如果我尝试AddParameterId不会附加到查询字符串上(我想因为它是 POST 而不是 GET),所以这是行不通的。API 不需要表单,它需要:

(string id, List<Dictionary<string,string>>)

我可以使用 a StringBuilder,但这感觉不对。我不确定这是否UrlSegment是最好的方法,因为我基本上会破解查询字符串。有没有办法使用 RestSharp 的 API 将我的请求格式化为我需要的格式?

restsharp

4
推荐指数
1
解决办法
7844
查看次数

发送 OAuth 令牌在 Postman 中有效,但在 RestSharp 中无效

我尝试使用 Postman 将不记名令牌发送到 Auth0 API,效果非常好。

然后我尝试使用 RestSharp (在 c# 中)进行相同的操作,但它根本不起作用。

下面是我的代码。我尝试了许多不同的格式,但没有一个能工作。我还有其他方法可以尝试使其工作吗?

var client = new RestClient("http://domain.auth0.com/api/v2/users");

RestRequest request = new RestRequest(Method.GET);
//request.AddHeader("authorization", "Bearer eyJhbGcJ9.eyJhdWQiOiJ6VU4hVWUE2.token");
//request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//request.AddHeader("Accept", "application/json");


//RestClient client = new RestClient("http://domain.auth0.com");
//RestRequest request = new RestRequest("api/v2/users", Method.GET);

request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("Authorization",
string.Format("Bearer " + "eyJhbGciOI1NiIsI9.eyJhdWQiOiWmVhTWpD2VycyI6eyJhY.token"),
            ParameterType.HttpHeader);

//request.AddParameter(&quot;Authorization&quot;,
//    String.Format(&quot;Bearer {0}&quot;, token),
//ParameterType.HttpHeader);
var response = client.Execute(request); 
Run Code Online (Sandbox Code Playgroud)

PS:令牌已更改。

restsharp oauth-2.0 auth0

4
推荐指数
1
解决办法
3845
查看次数

RestSharp 中的对象到 JSON 问题

我正在使用的 Rest API 有一个名为 Api-Key 的新字段。这不是有效的 C# 字段名称,因此我想知道是否有不同的方式来构建主体。

   var client = new RestClient("https://TestWeb");

        var request = new RestRequest("login", Method.POST);
        request.AddHeader("Content-type", "application/json");

        request.AddJsonBody(
           new {
               credentials =
            new
            {
                username = "Uname",
                password = "password",
                Api-Key = "apikey"
            } }); 
Run Code Online (Sandbox Code Playgroud)

c# restsharp

4
推荐指数
1
解决办法
3942
查看次数

如何使用 RestSharp 保存承载令牌以供将来使用

我对于自动化测试来说是一个相对菜鸟 - 目前,我正在使用 Postman 为 Visual Studio 2017 Enterprise 中的 API 请求生成 RestSharp 代码。

本质上,我正在创建一个基本的单元测试,然后放入代码来执行测试

我需要知道的是 - 在我的测试中,我是否可以首先进行登录调用并保存我的不记名令牌,以便在所有后续调用中使用 - 在邮递员中,这很容易,因为我可以设置一个环境将其拉入的变量。

所以本质上,我需要做的是进行登录调用以返回不记名令牌,保存不记名令牌,然后在我进行的后续调用中使用该令牌

任何帮助是极大的赞赏!

testing api rest restsharp

4
推荐指数
1
解决办法
4655
查看次数

在 C# 中使用 RestClient 作为 multipart/form-data 上传文件

我正在尝试使用restClient (RestSharp) 请求从 c# 类上传文件。我正在创建 Method.POST 方法并将一个音频文件添加到此请求中作为 multipart/form-data。

当我执行请求时服务器抛出异常。

例外 :

 System.ArgumentNullException: Value cannot be null.
 Parameter name: value
 at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
 at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
 at Groove.Libraries.Helper.EnumHelper.ParseEnum[T](String value) in D:\project\Groove\Web\Groove\Libraries\Helper\EnumHelper.cs:line 47
 at Groove.Controllers.Api.DocumentController. 
 <PostDocumentUpload>d__5.MoveNext()
Run Code Online (Sandbox Code Playgroud)

RestClient 请求代码:

string api_url = "http://localhost:57997/";
var fullFileName = "Adios.mp3";
var filepath = @"C:\Users\Admin\Desktop\Adios.mp3";

RestClient client = new RestClient(ApiModel.api_url);

var request = new RestRequest("api/document", Method.POST);
request.AddFile(Path.GetFileNameWithoutExtension(fullFileName), filepath);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("ReferenceType",28,ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

服务器代码: …

c# asp.net file-upload restsharp asp.net-web-api

4
推荐指数
1
解决办法
1万
查看次数

重定向期间在 RestSharp 中保留授权标头

我正在使用 RestSharp 进行 GET api 调用。api 调用通过传递授权标头通过 HTTP 基本身份验证进行身份验证。

服务器使用状态代码 307 重定向 api 调用。我的客户端代码确实处理重定向,但授权标头未传递到此重定向的 api 调用。这样做是出于此处提到的正当理由。因此我确实收到了未经授权的错误。

如何配置 RestClient 来恢复授权标头?

var client = new RestClient("https://serverurl.com");
var request = new RestRequest(Method.GET);

request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Tenant-Id", "4892");

IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Run Code Online (Sandbox Code Playgroud)

c# api unauthorized restsharp

4
推荐指数
1
解决办法
5694
查看次数

当响应头具有位置字段时,RestSharp返回空值

我的休息要求:

RestSharp.RestClient uplClient = new RestSharp.RestClient();
RestSharp.RestRequest request = new RestSharp.RestRequest(IMAGE_UPLOAD_URI, Method.POST);
request.AddParameter("user", USER_HASH);
request.AddParameter("apikey", API_KEY);
request.AddFile("Filedata", file, "test.jpg","image/jpeg");

uplClient.ExecuteAsync(request, (response) =>
 {
  callback(response.Content, null);

  if (response.StatusCode == HttpStatusCode.OK)
  {
   MessageBox.Show("Upload completed succesfully...\n" + response.Content);
  }
  else
  {
   MessageBox.Show(response.StatusCode + "\n" + response.StatusDescription);
  }
 });
Run Code Online (Sandbox Code Playgroud)

通过Fiddler检查响应时,原始数据是:

    HTTP/1.1 302 Moved Temporarily
    Server: nginx
    Date: Wed, 05 Dec 2012 11:35:57 GMT
    Content-Type: text/javascript;charset=utf-8
    Connection: keep-alive
    Location: file:///Applications/Install/8014A556-A76A-4294-B375-6E3668177CCA/Install/?errorNr=0&picUploadId=1007501
    Content-Length: 423

    {"ok":true,"error":false,"imageIcon":0,"uid":1287837,"tmpId":1007501,"url":"http:\/\/i1.ifrype.com\/tmp\/10\/1007501.jpg","urlIcon":"http:\/\/i1.ifrype.com\/tmp\/10\/i_1007501.jpg","urlSmall":"http:\/\/i1.ifrype.com\/tmp\/10\/sm_1007501.jpg","urlMiddle":"http:\/\/i1.ifrype.com\/tmp\/10\/nm_1007501.jpg","urlLarge":"http:\/\/i1.ifrype.com\/tmp\/10\/l_1007501.jpg","urlGM":"http:\/\/i1.ifrype.com\/tmp\/10\/ngm_1007501.jpg"}
Run Code Online (Sandbox Code Playgroud)

虽然RestSharp将所有respose值显示为null.我认为这与默认的JSonDeserializer有关,它无法解析某些数据.下面是成功解析响应的原始数据:

    HTTP/1.1 200 OK
    Server: nginx
    Date: Wed, 05 Dec 2012 11:28:13 …
Run Code Online (Sandbox Code Playgroud)

c# windows-phone-7 restsharp

3
推荐指数
1
解决办法
5250
查看次数

将奇怪的JSON响应解析为List <string>

我正在使用RestSharp调用Web服务并正确地恢复我的响应,但我得到的数据是一种奇怪的格式.它是一个GUIDS列表,这也是我想要的,但它们作为一个bool对象回来,看看:

"{
    \"5916DF70-C413-4132-90F7-C365B0FAA26D\" : true,
    \"B5F0FF80-F8D1-40F7-8313-045F02D37FAA\" : true,
    \"D859A904-EDAE-4D87-9ADC-8FB5F3B47B02\" : true
}"
Run Code Online (Sandbox Code Playgroud)

我将如何解析,所以我只得到一个只包含GUID的List?

c# asp.net json restsharp

3
推荐指数
1
解决办法
475
查看次数