locked
上传图片到Skydrive失败 RRS feed

  • 问题

  • 各位好,我借助RESTSharp 库,创建了一个通过调用Live REST api上传图片到Skydrive的工程,认证登录一切正常,但是在上传图片的时候报错了,错误为:

    Specified value has invalid HTTP Header characters.\r\nParameter name: name

    代码为:

      void UploadImage(string strStatus, string picPath, Action<bool, Exception> action)
            {
                var client = new RestClient("https://apis.live.net/v5.0/");
                client.Authenticator = new Oauth2(Token);
    
                var request = new RestRequest(Method.POST);
                request.Resource = "me/skydrive/files";
                request.RequestFormat = DataFormat.Json;
                request.DateFormat = DataFormat.Json.ToString();
                request.AddHeader("Authorization" , "Bearer " +Token);
    
                Require.Argument("name", picPath);
    
                var boundary = string.Concat("--", TwitterUtil.GenerateNonce());
                request.AddHeader("Content-Type", string.Concat("multipart/form-data; boundary=", boundary));
                request.AddHeader("Content-Disposition:", "form-data; name=\"file\"; filename=\"loli.jpg\"\r\n");
               
                if (!string.IsNullOrEmpty(picPath))
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(picPath))
                        {
                            using (var stream = store.OpenFile(picPath, FileMode.Open, FileAccess.Read))
                            {
                                byte[] bytes = new byte[stream.Length];
                                stream.Read(bytes, 0, bytes.Length);
                                request.AddFile("file", bytes, TwitterUtil.UrlEncode(picPath), "content");
                            }
                        }
                    }
                }
    
                var response = client.ExecuteAsync(request, (res) =>
                    {
                        var str = res.Content;
                        str = HttpUtility.HtmlDecode(str);
                        if (res.StatusCode == HttpStatusCode.OK)
                        {
                            action(true, null);
                        }
                        else
                        {
                            action(false, new Exception(str));
                        }
                    });
            }

    今天调了好长时间,实在是没辙才发帖求助的,先谢谢大家了。

    工程在此:http://sdrv.ms/SZtbRD

    2012年11月12日 12:52

全部回复

  • 补充一点,今天跟进去看了,发现Skydrive抛出的异常是在构造HTTP Header的地方,按照RFC的文档又重新写了下,还是报同样的异常

    request.AddHeader("Content-Disposition:", "form-data;\r\n name=\"file\";\r\n filename=\"loli.jpg\"\r\n");


    2012年11月12日 12:54