标签: restsharp

如何使用RestSharp执行get请求?

我在Windows Phone 7上使用RestSharp查明如何发出GET请求时遇到了麻烦.所有示例都显示发出POST请求,但我只需要GET.我该怎么做呢?

windows-phone-7 restsharp

6
推荐指数
2
解决办法
8047
查看次数

"错误:SendFailure(编写标题时出错)"

我正在尝试使用"rest sharp"软件包在"xamarin studio"中获取数据,如下所示:

var client = new RestClient ("https://xxxx.xxxx.com");

var request = new RestRequest ("/api/external/xxxx/xxxx/", Method.GET);
request.AddParameter("username", "xxxx");
request.AddParameter("api_key", "xxxx");

request.AddHeader("Content-type", "application/json");      
request.AddHeader("Accept", "text/plain");

request.RequestFormat = DataFormat.Json;

var response = client.Execute (request);

var model = JsonConvert.DeserializeObject<RootObject> (response.Content);
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

错误:SendFailure(写入标题时出错)

我真的不知道出了什么问题.我正在使用Windows 7 64位.

请帮忙...谢谢你提前!!!

c# android httpresponse restsharp xamarin

6
推荐指数
1
解决办法
3003
查看次数

使用RestSharp和ResponseWriter下载Stream

我使用ResponseWriter来装载使用RestSharp的流.

var client = new RestClient
var request = new RestRequest();
// ...
request.ResponseWriter = (ms) => {
  // how to detect the status code
};
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

如何在ResponseWriter中找到HTTP状态代码?有没有更好的下载流的方法?

http stream http-status-codes restsharp

6
推荐指数
1
解决办法
7547
查看次数

Json RestSharp deserilizing Response Data null

我使用RestSharp访问Rest API.我喜欢将数据作为POCO返回.我的RestSharp客户端看起来像这样:

var client = new RestClient(@"http:\\localhost:8080");
        var request = new RestRequest("todos/{id}", Method.GET);
        request.AddUrlSegment("id", "4");
        //request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        //With enabling the next line I get an new empty object of TODO
        //as Data
        //client.AddHandler("*", new JsonDeserializer());
        IRestResponse<ToDo> response2 = client.Execute<ToDo>(request);
        ToDo td=new JsonDeserializer().Deserialize<ToDo>(response2);

        var name = response2.Data.name;
Run Code Online (Sandbox Code Playgroud)

我的JsonObject类看起来像这样:

public class ToDo
{
    public int id;
    public string created_at;
    public string updated_at;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

和Json的回应:

{
    "id":4,
    "created_at":"2015-06-18 09:43:15",
    "updated_at":"2015-06-18 09:43:15",
    "name":"Another Random …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp

6
推荐指数
1
解决办法
5791
查看次数

RestSharp.NetCore中的ExecuteAsyncPost示例

我正在使用RestSharp.NetCore包,需要调用ExecuteAsyncPost方法.我正在努力理解回调参数.

    var client = new RestClient("url");
    request.AddParameter("application/json", "{myobject}",  ParameterType.RequestBody);
    client.ExecuteAsyncPost(request,**callback**, "POST");
Run Code Online (Sandbox Code Playgroud)

回调属于类型 Action<IRestResponse,RestRequestAsyncHandler>

有人请发一个小代码示例,演示如何使用回调参数和解释.

谢谢-C

.net c# callback restsharp asp.net-core-webapi

6
推荐指数
1
解决办法
6980
查看次数

RestSharp POST对象为JSON

这是我的课:

public class PTList
{
    private String name;

    public PTList() { }
    public PTList(String name)
    {
        this.name = name;
    }


    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}
Run Code Online (Sandbox Code Playgroud)

和我的RestSharp POST请求:

    protected static IRestResponse httpPost(String Uri, Object Data)
    {
        var client = new RestClient(baseURL);
        client.AddDefaultHeader("X-Authentication", AuthenticationManager.getAuthentication());
        client.AddDefaultHeader("Content-type", "application/json");
        var request = new RestRequest(Uri, Method.POST);

        request.RequestFormat = DataFormat.Json;

        request.AddJsonBody(Data);

        var response = client.Execute(request);
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

当我使用具有良好URI和PTList对象的httpPost方法时,前端API会回答“名称”为null。我认为我的PTList对象未在API请求中序列化为有效JSON,但无法理解出了什么问题。

c# json restsharp

6
推荐指数
1
解决办法
9143
查看次数

RestSharp将Datetimes转换为UTC

我有以下问题

我正在使用RestSharp来访问我的API.但是当我发送DateTime时,它似乎转换为UTC.我发送'10 .06.1991 00:00',API得到'09 .06.1991 22:00'

那么,当我的API获取DateTime对象时,我总是需要增加2个小时?

我检查了JSON RestSharp发送给API.

public class Test
{
    public int IntProperty {get;set;}
    public string StringProperty {get;set;}
    public DateTime DateTimeProperty {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我的目标是

Test t = new Test{ IntProperty=3, StringProperty="string", DateTimeProperty=new DateTime(1991,06,10) }
Run Code Online (Sandbox Code Playgroud)

当我通过RestSharp发送对象时,我的API接收的JSON是

{
    "IntProperty":3,
    "StringProperty":"string",
    "DateTimeProperty":"09.06.1991 22:00:00"
}
Run Code Online (Sandbox Code Playgroud)

知道我能做什么吗?谢谢

c# restsharp

6
推荐指数
1
解决办法
2949
查看次数

Windows身份验证 - Guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

我刚刚开始通过stackify使用Retrace来监控我的应用程序,并且看到了数以千计的错误:

System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
at System.Guid.TryParseGuidWithNoStyle
at System.Guid.TryParseGuid
at System.Guid..ctor
at System.DirectoryServices.AccountManagement.ADStoreCtx.IdentityClaimToFilter
Run Code Online (Sandbox Code Playgroud)

这些错误每天发生数千次,我无法理解为什么?我很乐意发布更多代码,如果它有助于确定问题,但是现在我将发布我认为相关的内容,请不要在没有给我机会在被要求时提供更多必要信息的情况下投票.

首先,我的应用程序如下工作:

MVC前端 - 使用Windows身份验证(使用RestSharp来调用后端)

Web API后端,使用从RestSharp NTLM身份验证传递的Windows身份验证.

RestSharp包装器

    public object WebRequest<T>(string controller, Dictionary<string, string> parameters, Method apiMethod, string action)
    {
        RestClient client = new RestClient(Url + controller + "/");
        client.Authenticator = new NtlmAuthenticator();
        RestRequest request = new RestRequest(action, apiMethod);

        if (parameters != null && parameters.Count > 0)
        {
            foreach (var parameter in parameters)
            {
                request.AddParameter(parameter.Key, parameter.Value);
            }
        }

        object result …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net asp.net-mvc restsharp

6
推荐指数
1
解决办法
738
查看次数

Restsharp 返回 403 而 Postman 返回 200

这是 Postman 为成功调用我的页面而提供的(修改后的)片段。

var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Basic anVyYTp3MmZacmo2eGtBOHJsRWrt");
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

但是当放置在我的 c# 应用程序中时,它返回 403 forbidden,而 Postman 生成它并收到 200。当我在我的应用程序中使用 httpclient 时会发生同样的事情(403)。

c# httpclient restsharp wp-api

5
推荐指数
1
解决办法
2974
查看次数

Nancy:解析​​“multipart/form-data”请求

我有一个 RestSharp 客户端和 Nancy Self Host Server。我想要的是

从客户端发送多部分表单数据并从服务器轻松解析该数据:

从 RestSharp 客户端发送二进制文件和 Json 数据作为多部分表单数据,并能够从 Nancy 服务器获取二进制文件和 Json 对象

使用 Restsharp 的客户端:[ http://restsharp.org/ ] 我尝试发送“multipart/form-data”请求,其中包含一个二进制文件和一些 json 格式的元数据:

var client = new RestClient();
...

IRestRequest restRequest = new RestRequest("AcmeUrl", Method.POST);

restRequest.AlwaysMultipartFormData = true;
restRequest.RequestFormat = DataFormat.Json;

// I just add File To Request
restRequest.AddFile("AudioData", File.ReadAllBytes("filePath"), "AudioData");

// Then Add Json Object
MyObject myObject = new MyObject();
myObject.Attribute ="SomeAttribute";
....

restRequest.AddBody(myObject);

client.Execute<MyResponse>(request);
Run Code Online (Sandbox Code Playgroud)

在使用 Nancy[ http://nancyfx.org/ ] 的服务器上,尝试获取文件和 Json 对象 [元数据]

// …
Run Code Online (Sandbox Code Playgroud)

rest restsharp nancy

5
推荐指数
1
解决办法
2231
查看次数