我正在使用RestSharp来使用REST样式的服务.这个库很新,并且会对如何使用RestSharp实现分页有一些指导.
关于如何实现这一点的任何现有例子?
RestSharp - http://restsharp.org/
谢谢
我正在制作一个使用RestSharp下载一些数据的wp7应用程序.我注意到应用程序指南要求我提供一个允许用户取消数据传输的ui元素.是否可以取消具有rest sharp的ExecuteAsync请求?
我正在使用RestSharp与远程服务器通信.我收到一个JSON序列化字符串,我可以反序列化为ac#对象.我也能够将json数组反序列化为List.但是,我希望这些对象在WPF绑定中使用,所以我需要将它们放在ObservableCollection中以方便使用.但是,如果我尝试将属性从List更改为ObservableCollection(或IList,或ICollection或Collection),则会在反序列化时出现异常.
Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]
Run Code Online (Sandbox Code Playgroud)
底层代码真的不是特别的,但无论如何它在这里:
private ObservableCollection<StationDto> stations;
[JsonProperty(PropertyName = "stations")]
public ObservableCollection<StationDto> Stations
{
get { return this.stations; }
set
{
this.stations = value;
RaisePropertyChanged(() => Stations);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道接口不起作用,因为Json.net需要一个具体的类来序列化.
我已经做了相当多的谷歌搜索,但我还没有看到解决方案.是否有一种常用于json/rest服务的手工代理的模式?
我正在尝试在全新安装的Windows 8.1中使用Visual Studio 2012 Express中的RestSharp.我试图使用的API仅支持RC4-SHA用于SSL.证书有效.
var client = new RestClient();
client.BaseUrl = "https://teststore.mybigcommerce.com/api/v2/";
client.Authenticator = new HttpBasicAuthenticator("username", "key");
var request = new RestRequest();
request.Resource = "time.json";
IRestResponse response = client.Execute(bcrequest);
Run Code Online (Sandbox Code Playgroud)
我不断收到客户的错误:The request was aborted: Could not create SSL/TLS secure channel.我认为存在证书问题,直到我最终采取数据包捕获并发现没有共同的密码套件.RC4-SHA在客户端不可用.安装Windows 7并运行完全相同的代码后,问题就消失了.
我的应用程序要求是使用签名程序集.我使用Visual Studio 2012命令工具签署了RestSharp程序集.应用程序构建成功,但运行时,会出现以下错误:
无法加载文件或程序集'RestSharp,Version = 104.4.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.需要一个强名称的程序集.(HRESULT异常:0x80131044)
这有什么解决方案吗?
我试图反序列化我从休息服务(使用RestSharp)获得的json响应,但我不断收到此错误:
无法将类型为'RestSharp.JsonArray'的对象强制转换为'System.Collections.Generic.IDictionary`2 [System.String,System.Object]'.
响应如下:
“message” : “Successfully authenticated.”,
“total” : null,
“data” : [ {
“token” : “XYZ”,
“user” : {
“password” : “ABC”,
“username” : “User”
}
}],
“success” : true
Run Code Online (Sandbox Code Playgroud)
我创建了以下C#对象:
internal class AuthenticationResponse
{
public string message { get; set; }
public string total { get; set; }
public AuthenticationResponseData data { get; set; }
public bool success {get;set;}
}
internal class AuthenticationResponseData
{
public string token { get; set; }
public AuthenticationUser user {get;set;}
}
internal …Run Code Online (Sandbox Code Playgroud) 我根据RestSharp.org文档尝试了所有可能的场景,但没有运气!
我这里有ASP.Net Web API是POST资源
[Route("/api/saveperson/{name}/{fathername}")]
public void Post([FromBody]CustomObject customObject, string name, string fatherName)
{
//customObject is null
}
Run Code Online (Sandbox Code Playgroud)
RestSharp请求:
public void SomeAPIRequest()
{
var baseUrl = "someurl from config";
var client = new RestClient(baseUrl);
var request = new RestRequest("/api/saverperson/{name}/{fathername}",Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(myObject); //This object is perfectly serialized in json
request.AddParameter("name","Gaurav",ParameterType.UrlSegment);
request.AddParameter("fathername","Lt. Sh. Ramkrishan",ParameterType.UrlSegment);
var response= client.Execute(request);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码在Body中发布的参数始终为null.
当我进行以下调用时,Body中发布的参数具有值,但其他值为空
public void SomeAPIRequest()
{
var baseUrl = "someurl from config";
var client = new RestClient(baseUrl);
var request = new RestRequest("/api/saverperson/{name}/{fathername}",Method.POST); …Run Code Online (Sandbox Code Playgroud) 我在Windows Phone 8.1应用程序中使用RestSharp.当服务器返回响应的代码比200不同的RESTClient实现抛出异常维基说,我应该得到的响应与正确的状态码.我想得到响应的内容,因为服务器返回错误消息.
private async Task<T> ExecuteAsync<T>(IRestRequest request)
{
if (!_networkAvailableService.IsNetworkAvailable)
{
throw new NoInternetException();
}
request.AddHeader("Accept", "application/json");
IRestResponse<T> response;
try
{
response = await _client.Execute<T>(request); //here I get exception
}
catch (Exception ex)
{
throw new ApiException();
}
HandleApiException(response);
return response.Data;
}
private void HandleApiException(IRestResponse response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
return;
}
//never reach here :(
ApiException apiException;
try
{
var apiError = _deserializer.Deserialize<ApiErrorResponse>(response);
apiException = new ApiException(apiError);
}
catch (Exception)
{
throw new ApiException(); …Run Code Online (Sandbox Code Playgroud) 我正在使用RestSharp来使用REST Web服务。我实现了自己的Response对象类,以与RestSharp中集成的自动序列化/反序列化一起使用。
我还添加了一个可以正常工作的枚举映射。
此类的问题是,当我发送正确的请求时,我会返回正确的响应,因此Response.Content包含了我期望的内容,但是反序列化过程无法正常工作。
响应内容
{
"resultCode": "SUCCESS",
"hub.sessionId": "95864537-4a92-4fb7-8f6e-7880ce655d86"
}
Run Code Online (Sandbox Code Playgroud)
该ResultCode属性已正确映射到ResultCode.SUCCESS枚举值,但该HubSessionId属性始终为,null因此似乎未进行反序列化。
我看到的唯一可能的问题是带有'。'的JSON PropertyName。在名字里。可能是问题吗?这与不是Newtonsoft.Json的新JSON序列化程序有关吗?我该如何解决?
更新
我发现Json Attributes被完全忽略,[JsonConverter(typeof(StringEnumConverter))]。因此,我认为枚举映射由默认的Serializer自动执行,没有任何属性。“ hub.sessionId”属性的问题仍然存在。
这是我的代码
public class LoginResponse
{
[JsonProperty(PropertyName = "resultCode")]
[JsonConverter(typeof(StringEnumConverter))]
public ResultCode ResultCode { get; set; }
[JsonProperty(PropertyName = "hub.sessionId")]
public string HubSessionId { get; set; }
}
public enum ResultCode
{
SUCCESS,
FAILURE
}
// Executes the request and deserialize the JSON to the corresponding
// Response object …Run Code Online (Sandbox Code Playgroud) 使用下面的简单代码,RestClient无法通过Web代理访问。它不尊重请求的价值。
我可以在浏览器中访问要查询的网站;但是RestClient被我公司的代理阻止了。
try
{
RestClient client = new RestClient("http://services.groupkt.com/country/get/all");
RestRequest request = new RestRequest(Method.POST);
//set credentials to default
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
//also tried using client.UserAgent to spoof Firefox user-agent to no avail
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
IRestResponse response = client.Execute(request);
textBox1.Text = response.Content;
}
catch (Exception ex)
{
textBox1.Text = "ERROR:" + ex.Message;
}
Run Code Online (Sandbox Code Playgroud)
所以我在textBox1中最终得到的是html,它呈现为:
仅授权
Secure Web Gateway已阻止您的请求,因为您尚未获得授权并且需要授权。
网址:
用户名/来源:/ 10.xx.xx.xx
规则集:使用Kerberos和NTLM后备身份验证进行身份验证/身份验证
使用Kerberos(不评估NTLM令牌)
重要说明:当您访问Internet网页时,您应该遵守[CompanyName] Internet过滤的授权批准。
产生2017-05-05 15:04:08
RestSharp 104.1.0.0
换句话说,RestSharp不会像默认的那样将默认凭据传输到Web代理。
restsharp ×10
c# ×5
json ×2
.net ×1
.net-4.0 ×1
http-proxy ×1
json.net ×1
paging ×1
rest ×1
ssl ×1
strongname ×1
windows-8.1 ×1