当我尝试使用nuget将RestSharp添加到可移植类库项目时,我得到以下内容:
无法安装包'RestSharp 104.1'.您正在尝试将此软件包安装到以".NETPortable,Version = v4.0,Profile = Profile104"为目标的项目中,但该软件包不包含与该框架兼容的任何程序集引用或内容文件.有关更多信息,请与软件包作者联系.
我假设它不受支持?如果是这种情况,任何人都有任何关于如何使其工作的建议?
我对 RestClient 响应返回有疑问
“StatusCode: 0,Content-Type: , Content-Length: )”,ErrorMessage 为“由于配置的 HttpClient.Timeout 已过 100 秒,请求被取消”。
尽管只有 170KB 的数据,但由于其最终优化不佳,该请求可能需要 5 分钟以上的时间。
var client = new RestClient(url);
RestRequest request = new RestRequest() { Method = Method.Get };
request.Timeout = 300000;
request.AddParameter("access_token", AccessToken);
request.AddParameter("start_date", StartDate.ToString("yyyy-MM-dd"));
request.AddParameter("end_date", EndDate.ToString("yyyy-MM-dd"));
request.AddParameter("offset", offset.ToString());
var response = await client.ExecuteAsync(request);
var responseWorkLoads = JObject.Parse(response.Content).SelectToken("worklogs");
Run Code Online (Sandbox Code Playgroud) IRestResponse我有以下问题:
public async Task<CezanneToken> GetAccessToken()
{
var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);
IRestResponse response = await client.ExecuteAsync(request);
string serStatus = ((RestResponseBase)response).Content;
CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
string Token = details.access_token;
return details;
}
Run Code Online (Sandbox Code Playgroud)
IRestResponse投掷
找不到类型或命名空间名称“IRestResponse”(您是否缺少 using 指令或程序集引用?)我无法使其工作。IntelliSense 建议使用
RestResponse> 而不是IRestResponse。
但当我去的时候,RestResponse我得到Bad Request了回应。
上面的代码示例是从 Visual Basic“翻译”而来的,但它在 VB 中运行得很好。我不知道问题是否来自Bad Request使用 …
我有以下代码:
public void GetJson()
{
RestRequest request = new RestRequest(Method.GET);
var data = Execute<Dictionary<string, MyObject>>(request);
}
public T Execute<T>(RestRequest request) where T : new()
{
RestClient client = new RestClient(baseUrl);
client.AddHandler("text/plain", new JsonDeserializer());
var response = client.Execute<T>(request);
return response.Data;
}
Run Code Online (Sandbox Code Playgroud)
问题是有时响应将是一个空的json数组[].当我运行此代码时,我得到以下异常:无法将类型为'RestSharp.JsonArray'的对象强制转换为'System.Collections.Generic.IDictionary`2 [System.String,System.Object]'.
有没有办法优雅地处理这个?
我希望能够发布一个文件,并作为该帖子的一部分添加数据.
这是我有的:
var restRequest = new RestRequest(Method.POST);
restRequest.Resource = "some-resource";
restRequest.RequestFormat = DataFormat.Json;
string request = JsonConvert.SerializeObject(model);
restRequest.AddParameter("text/json", request, ParameterType.RequestBody);
var fileModel = model as IHaveFileUrl;
var bytes = File.ReadAllBytes(fileModel.LocalStoreUrl);
restRequest.AddFile("FileData", bytes, "file.zip", "application/zip");
var async = RestClient.ExecuteAsync(restRequest, response =>
{
if (PostComplete != null)
PostComplete.Invoke(
new Object(),
new GotResponseEventArgs
<T>(response));
});
Run Code Online (Sandbox Code Playgroud)
它发布文件很好,但数据不存在 - 这甚至可能吗?
[UPDATE]
我修改了代码以使用多部分标题:
var restRequest = new RestRequest(Method.POST);
Type t = GetType();
Type g = t.GetGenericArguments()[0];
restRequest.Resource = string.Format("/{0}", g.Name);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("content-type", "multipart/form-data");
string …Run Code Online (Sandbox Code Playgroud) 我以JSON格式启动此RestSharp查询:
var response = restClient.Execute<Report>(request);
Run Code Online (Sandbox Code Playgroud)
我得到的回复包含这些数据
[
{
"Columns":
[
{"Name":"CameraGuid","Type":"Guid"},
{"Name":"ArchiveSourceGuid","Type":"Guid"},
{"Name":"StartTime","Type":"DateTime"},
{"Name":"EndTime","Type":"DateTime"},
{"Name":"TimeZone","Type":"String"},
{"Name":"Capabilities","Type":"UInt32"}
],
"Rows":
[
[
"00000001-0000-babe-0000-00408c71be50",
"3782fe37-6748-4d36-b258-49ed6a79cd6d",
"2013-11-27T17:52:00Z",
"2013-11-27T18:20:55.063Z",
"Eastern Standard Time",
2147483647
]
]
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其反序列化为这组类:
public class Report
{
public List<ReportResult> Results { get; set; }
}
public class ReportResult
{
public List<ColumnField> Columns { get; set; }
public List<RowResult> Rows { get; set; }
}
public class ColumnField
{
public string Name { get; set; }
public string Type { get; …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我使用RestSharp查询REST API和System.Net.Mail来发送电子邮件.在程序启动时,我设置了ServicePointManager.SecurityProtocol属性.
如果我将属性设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
Run Code Online (Sandbox Code Playgroud)
使用RestSharp查询API时抛出异常:
The request was aborted: Could not create SSL/TLS secure channel
Run Code Online (Sandbox Code Playgroud)
如果我将属性设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
Run Code Online (Sandbox Code Playgroud)
使用System.Net.Mail发送电子邮件时抛出异常:
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
从使用API Docusign,Twilio和Auth0.所有3个都RestSharp.dll具有依赖性.
如果我使用RestSharp.dll包含在Docusign包装,Docusign效果很好,但Auth0并Twillio给出错误:
无法加载文件或程序集'RestSharp,Version = 104.1.0.0,Culture = neutral,PublicKeyToken = null'
如果我使用普通RestSharp.dll(Install-Package RestSharp),Twilio并且Auth0工作正常,但在使用Docusign时出现错误:
无法加载文件或程序集'RestSharp,Version = 100.0.0.0,Culture = neutral,PublicKeyToken = 5xxxxxxxxxxxx'
添加绑定重定向并不能解决问题.没有绑定重定向,我在日志中收到此错误:
比较程序集名称导致不匹配:MAJOR VERSION.
如果我使用绑定重定向:
比较程序集名称导致不匹配:PUBLIC KEY TOKEN.
绑定重定向代码:
<dependentAssembly>
<assemblyIdentity name="RestSharp" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-105.2.3.0" newVersion="105.2.3.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud) 我试图RestSharp在我的C#Visual-Studio 2013项目中使用POST给定URL的数据.当我尝试通过NuGet安装包时,它给我以下错误:
Installing 'RestSharp 106.1.0'.
Successfully installed 'RestSharp 106.1.0'.
Adding 'RestSharp 106.1.0' to WebApplicationJson.
Uninstalling 'RestSharp 106.1.0'.
Successfully uninstalled 'RestSharp 106.1.0'.
Install failed. Rolling back...
Could not install package 'RestSharp 106.1.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Run Code Online (Sandbox Code Playgroud)
至于我已经阅读GitHub上这主要是一个问题.NetPortable的框架,所以我的想法.我也尝试将我的框架版本更改为3.5但仍然是相同的错误.
有没有人遇到类似的问题?
如果您需要更多信息,请发表评论.
使用 .net 6.0 和 RestSharp 110.0.1 当我按照 RestSharp 文档(下面链接)中的示例进行操作时,我收到错误“RestClient.Authenticator 无法分配给 - 它是只读的。”
RestSharp 文档 GitHub Gist 获取文档代码
public class WebInteractionsAuthenticator : AuthenticatorBase {
readonly string _baseUrl;
readonly string _clientId;
readonly string _clientSecret;
public WebInteractionsAuthenticator(string baseUrl, string clientId, string clientSecret) : base("") {
_baseUrl = baseUrl;
_clientId = clientId;
_clientSecret = clientSecret;
}
protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken) {
Token = string.IsNullOrEmpty(Token) ? await GetToken() : Token;
return new HeaderParameter(KnownHeaders.Authorization, Token);
}
async Task<string> GetToken() {
var options = new …Run Code Online (Sandbox Code Playgroud) restsharp ×10
c# ×8
.net ×4
access-token ×1
asp.net ×1
asp.net-core ×1
docusignapi ×1
rest ×1
servicestack ×1
timeout ×1