我正在使用带有改装的多部分。我尝试为我的服务上传个人资料图片,邮递员生成的代码看起来像这样
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 偏移量,System.Int32 大小)[0x000d2] in :0 at System.Net.WebConnectionStream.ReadAll () [0x0010e] in :0 at System.Net.HttpWebResponse.ReadAll () [0x00011] in :0 at System。 Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult 结果) [0x001d6] in :0 在 System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData 数据) [0x0013e] in :0 在 System.Net.WebConnection.ReadDone (System.IAsyncResult 结果) [0x0024d] 在 :0 在 System.Net.Sockets.SocketAsyncResult+<>c.b__27_0 (System.Object 状态) [0x00000] 在 :0 在 System.Threading .QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00015] 在 System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] 在 :0 在 ObjCRuntime.Runtime.ThreadPoolDispatcher (System.Func`1[TResult] 回调) [0x00006] in <0b60c1467e7449608ac42f9c7bbfdd05>:0 at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00009] in:0 at System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String其中)[0x00031] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin。iOS/mcs/class/System/System.Net/WebConnection.cs:439
. 如果我使用Stream
或者StreamPart
它也会抛出异常,表示流已关闭。