我希望能够发布一个文件,并作为该帖子的一部分添加数据.
这是我有的:
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) 我有这样的xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
<item>
<accountid>1</accountid>
<accounttypeid>1</accounttypeid>
<accounttypename/>
<accountbankid>1</accountbankid>
<accountbankname/>
<accountsaldo>0</accountsaldo>
</item>
<item>
<accountid>2</accountid>
<accounttypeid>1</accounttypeid>
<accounttypename/>
<accountbankid>2</accountbankid>
<accountbankname/>
<accountsaldo>0</accountsaldo>
</item>
...
</xml>
Run Code Online (Sandbox Code Playgroud)
我想将这个xml列表反序列化为POCO对象
public class Account
{
public string AccountId { get; set; }
public string AccountTypeId { get; set; }
public string AccountTypeName { get; set; }
public string AccountBankId { get; set; }
public string AccountBankName { get; set; }
public string AccountSaldo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我找到了很棒的产品RestSharp与rest客户端一起工作.我想使用它的解串器,我尝试了两种方法.
1)我试过了
request.RootElement = "item";
var response …
我正在使用Visual Studio C#2010 Express并正在学习.RESTSharp应该由NuGet安装,但NuGet不能与Express版本一起使用.我还可以手动安装和配置RESTSharp吗?
var client = new RestClient("http://10.0.2.2:50670/api");
var request = new RestRequest("Inventory", Method.GET);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
// execute the request to return a list of InventoryItem
RestResponse<JavaList<InventoryItem>> response = (RestResponse<JavaList<InventoryItem>>)client.Execute<JavaList<InventoryItem>>(request);
Run Code Online (Sandbox Code Playgroud)
返回的内容是JSON字符串,即对象数组.以下是它的简短摘录:
[{"Id":1,"Upc":"1234567890","Quantity":100,"Created":"2012-01-01T00:00:00","Category":"Tequila","TransactionType":"Audit","MetaData":"PATRON 750ML"},{"Id":2,"Upc":"2345678901","Quantity":110,"Created":"2012-01-01T00:00:00","Category":"Whiskey","TransactionType":"Audit","MetaData":"JACK DANIELS 750ML"},{"Id":3,"Upc":"3456789012","Quantity":150,"Created":"2012-01-01T00:00:00","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"}]
Run Code Online (Sandbox Code Playgroud)
错误消息:
由于对象的当前状态,操作无效
这有什么不对?我InventoryItem的属性与JSON字符串中的每个对象相同.我错过了一步吗?
我正在努力让RestSharp与我拥有的宁静服务一起工作.一切似乎工作得很好,除非我传递的对象POST包含一个列表(在这个特殊情况下是一个列表string).
我的目标:
public class TestObj
{
public string Name{get;set;}
public List<string> Children{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
当它被发送到服务器时,Children属性将作为包含内容的字符串发送System.Collections.Generic.List`1[System.String].
这是我发送对象的方式:
var client = new RestClient();
var request = new RestRequest("http://localhost", Method.PUT);
var test = new TestObj {Name = "Fred", Children = new List<string> {"Arthur", "Betty"}};
request.AddObject(test);
client.Execute<TestObj>(request);
Run Code Online (Sandbox Code Playgroud)
我做错了什么,或者这是RestSharp中的错误?(如果它有所不同,我使用的是JSON,而不是XML.)
我试图将json响应从foursquare变回对象.我得到这样的东西了
{
"meta":{
"code":200
},
"response":{
"venues":[
{
"id":"4abfb58ef964a520be9120e3",
"name":"Costco",
"contact":{
"phone":"6045967435",
"formattedPhone":"(604) 596-7435"
},
"location":{
"address":"7423 King George Hwy",
"crossStreet":"btw 76 Avenue & 73A Avenue",
"lat":49.138259617056015,
"lng":-122.84723281860352,
"distance":19000,
"postalCode":"V3W 5A8",
"city":"Surrey",
"state":"BC",
"country":"Canada",
"cc":"CA"
},
"canonicalUrl":"https:\/\/foursquare.com\/v\/costco\/4abfb58ef964a520be9120e3",
"categories":[
{
"id":"4bf58dd8d48988d1f6941735",
"name":"Department Store",
"pluralName":"Department Stores",
"shortName":"Department Store",
"icon":{
"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/shops\/departmentstore_",
"suffix":".png"
},
"primary":true
}
],
"verified":true,
"restricted":true,
"stats":{
"checkinsCount":2038,
"usersCount":533,
"tipCount":12
},
"url":"http:\/\/www.costco.ca",
"specials":{
"count":0,
"items":[
]
},
"hereNow":{
"count":0,
"groups":[
]
},
"referralId":"v-1366316196"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我做了这样一堂课
public class Response …Run Code Online (Sandbox Code Playgroud) 如何在Https请求中添加RestSharp添加客户端证书?我的代码不起作用.
public static IRestResponse<User> AsyncHttpRequestLogIn(string path, string method, object obj)
{
var client = new RestClient(Constants.BASE_URL + path); // https:....
var request = method.Equals("POST") ? new RestRequest(Method.POST) : new RestRequest(Method.GET);
request.RequestFormat = RestSharp.DataFormat.Json;
// The path to the certificate.
string certificate = "cer/cert.cer";
client.ClientCertificates.Add(new X509Certificate(certificate));
request.AddBody(
obj
);
IRestResponse<User> response = client.Execute<User>(request);
return response;
}
Run Code Online (Sandbox Code Playgroud) 当使用RestSharp调用API时,我收到此错误:
底层连接已关闭:发送时发生意外错误.
我已经验证我的客户端ID,密码,用户名和密码是否正确.我能够在没有PowerShell问题的情况下做到这一点.
public string GetTokenForBrightIdea()
{
RestClient restclient = new RestClient(_uri);
RestRequest request = new RestRequest() { Method = Method.POST };
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", _clientId);
request.AddParameter("client_secret", _clientSecret);
request.AddParameter("username", _clientUsername);
request.AddParameter("password", _clientPassword);
var tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
return JsonConvert.DeserializeObject<Dictionary<string, object>>(
responseJson)["access_token"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
使用RestSharp进行此操作时我错过了什么?
我在 Visual Studio 中有一个 C#(Azure 函数应用程序)项目。.Net 6.0
我将 RestSharp 更新到版本 110.2.0,现在出现此错误:
[2023-04-29T21:34:10.399Z] Executed 'RequestItemsPage' (Failed, Id=8eefbb7c-3bc0-44db-81e4-14c56096fc81, Duration=63ms)
[2023-04-29T21:34:10.401Z] System.Private.CoreLib: Exception while executing function: RequestItemsPage. System.Private.CoreLib: Exception has been thrown by the target of an invocation. RestSharp: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
[2023-04-29T21:34:10.408Z] eb4802876b9e44839f63422be68619dc: Function 'RequestItemsPage (Activity)' failed with an error. Reason: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
[2023-04-29T21:34:10.411Z] ---> System.IO.FileNotFoundException: Could not load …Run Code Online (Sandbox Code Playgroud) 我很困惑我应该使用哪个工厂OAuth1Authenticator工厂方法.我想我应该得到一个消费者秘密令牌(我可以用RestSharp获取吗?),然后使用OAuth1Authenticator.ForRequestToken,然后获取访问令牌和秘密访问令牌(如何?),然后使用OAuth1Authenticator.ForAccessToken并使用此返回值向前看.
但似乎RestSharp被设计为使用唯一的一个身份验证器,我似乎找不到从冷启动(只有app令牌)到拥有所有必要凭据(消费者密钥和秘密,访问密钥和秘密)的方法.
奖金问题:
restsharp ×10
c# ×8
rest ×3
.net ×2
json ×2
api ×1
foursquare ×1
oauth ×1
servicestack ×1
ssl ×1