我正在将 multipart 与 Refit 一起使用。我尝试为我的服务上传个人资料图片,邮递员生成的代码看起来像这样
var client = new RestClient("http://api.example.com/api/users/1");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "xxx");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_method\"\r\n\r\nput\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"profile_picture\"; filename=\"ic_default_avatar.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
然后我像这样构造 Refit 方法
[Multipart]
[Post("/users/{id}")]
IObservable<BaseResponse<User>> UpdateProfilePicture(int id,[AliasAs("profile_picture")] byte[] profilePicture,[AliasAs("_method")]string method="put");
如果我使用byte[]否则ByteArrayPart它会抛出异常
{System.Net.Http.HttpRequestException:发送请求时出错---> System.Net.WebException:获取响应流时出错(分块Read2):ReceiveFailure ---> System.Exception:在System.Net.WebConnection .HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String where) [0x00031] in :0 at System.Net.WebConnection.Read (System.Net.HttpWebRequest request, System.Byte[] buffer, System .Int32 offset, System.Int32 size) [0x000d2] in :0 at System.Net.WebConnectionStream.ReadAll () [0x0010e] in :0 at System.Net.HttpWebResponse.ReadAll () [0x00011] in :0 at System.
. 如果我使用Stream
orStreamPart
它也会抛出异常表示流已关闭。
慕婉清6462132
郎朗坤
相关分类