代码之家  ›  专栏  ›  技术社区  ›  MrLister

如何使用RESTSharp和.net 4.0将文件上传到rackspace?

  •  3
  • MrLister  · 技术社区  · 9 年前

    以下是我迄今为止所做的,但没有奏效:

        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:(在网上找到这个),它会给我一个令牌……现在我该怎么办?

        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和令牌 此数据将传递给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);
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   MrLister    9 年前

    请参阅上面的帖子,了解导致这一事件的支持功能

       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;
        }