gpt4 book ai didi

.net-4.0 - 如何使用 RESTSharp 和 .net 4.0 将文件上传到机架空间?

转载 作者:行者123 更新时间:2023-12-02 19:28:29 25 4
gpt4 key购买 nike

这是我到目前为止所拥有的,但它不起作用:

    private void _send1(string file)
{
var client = new RestClient("https://identity.api.rackspacecloud.com/v2.0");
var request = new RestRequest("tokens", Method.POST);
request.RequestFormat = DataFormat.Json;

string test = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\"{\"username\":\"";
test += UserName;
test += "\",\"apiKey\":\"";
test += MyToken;
test += "\"}}}";

request.AddBody(serText);
request.AddParameter("application/json", test, ParameterType.RequestBody);

RestResponse response = (RestResponse)client.Execute(request);

// Content = "{\"badRequest\":{\"code\":400,\"message\":\"java.lang.String cannot be cast to org.json.simple.JSONObject\"}}"
}

注意:用户名和 apiKey 是有效的 RackSpace 凭据:-)

谢谢提前

尝试 2:(在网上找到这个)它给了我一个 token ...现在我该怎么办?

    private void _send2(string file)
{
Dictionary<string, object> dictAuth = new Dictionary<string, object>();
dictAuth.Add("RAX-KSKEY:apiKeyCredentials", new { username = UserName, apiKey = MyToken });
var auth = new
{
auth = dictAuth
};

RestClient client = new RestClient("https://identity.api.rackspacecloud.com");
RestSharp.RestRequest r = new RestRequest("/v2.0/tokens", Method.POST);
r.AddHeader("Content-Type", "application/json");
r.RequestFormat = DataFormat.Json;
r.AddBody(auth);


RestResponse response = (RestResponse)client.Execute(r);
// Content = "{\"access\":{\"token\":{\"id\":\"AACCvxjTOXA\",\"expires\":\"2016-04-09T21:12:10.316Z\",\"tenant\":{\"id\":\"572045\",\"name\...
}

再进一步:我创建了一个类,用于解析上面第 2 步中的 URL、tenantID 和 token 此数据将传递给 PostFile 调用:

    private void PostFile(string url, string tenantID, string token, string file)
{
string fName = Path.GetFileName(file);

RestClient client = new RestClient(url);
string baseURL = string.Format("v1/{0}/Support/{1}", tenantID, fName);

RestRequest r = new RestRequest(baseURL, Method.POST);
r.AddHeader("Content-Type", "text/plain");
r.AddParameter("X-Auth-Token", token);

r.AddFile(fName, file);

RestResponse response = (RestResponse)client.Execute(r);

if( response.StatusCode == System.Net.HttpStatusCode.OK)
{
int x = 0;
}

}

这是最终起作用的:

        bool bRetval = false;
string fName = Path.GetFileName(file);

RestClient client = new RestClient(url);
string baseURL = string.Format("/Support/{0}", fName);

RestRequest r = new RestRequest(baseURL, Method.PUT);
r.AddHeader("Content-Type", "text/plain");
r.AddHeader("X-Auth-Token", token);

r.AddFile(fName, file);

RestResponse response = (RestResponse)client.Execute(r);

最佳答案

请参阅上面的帖子,了解导致此问题的支持功能

   private bool PostFile(string url, string token, string file)
{
bool bRetval = false;
string fName = Path.GetFileName(file);

RestClient client = new RestClient(url);
string baseURL = string.Format("/Support/{0}", fName);

RestRequest r = new RestRequest(baseURL, Method.PUT);
r.AddHeader("Content-Type", "text/plain");
r.AddHeader("X-Auth-Token", token);

r.AddFile(fName, file);

RestResponse response = (RestResponse)client.Execute(r);

if ( response.StatusCode == System.Net.HttpStatusCode.Created)
{
bRetval = true;
}

return bRetval;
}

关于.net-4.0 - 如何使用 RESTSharp 和 .net 4.0 将文件上传到机架空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36509279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com