C# HttpClient.SendRequestAsync触发ArgumentException
我一直在试图上传一张照片到服务器,只要用户想改变它的头像 从拍摄的照片开始,一切都很好,将照片放入C# HttpClient.SendRequestAsync触发ArgumentException,c#,windows-phone-8.1,C#,Windows Phone 8.1,我一直在试图上传一张照片到服务器,只要用户想改变它的头像 从拍摄的照片开始,一切都很好,将照片放入irandomaccesstream和HttpMultipartFormDataContent中,但当我尝试发送请求时,会抛出一个ArgumentException 就我在debug上看到的情况而言,似乎没有什么问题,但无论如何都会抛出异常 代码如下: public static async Task<WebResult> UploadImage( WebHost webHost, Ob
irandomaccesstream
和HttpMultipartFormDataContent
中,但当我尝试发送请求时,会抛出一个ArgumentException
就我在debug上看到的情况而言,似乎没有什么问题,但无论如何都会抛出异常
代码如下:
public static async Task<WebResult> UploadImage( WebHost webHost, Object webPath, String webQuery, IRandomAccessStream imageStream ) {
/* ... */
HttpClient
httpClient = new Windows.Web.Http.HttpClient();
HttpMultipartFormDataContent
httpMultipartContent = new HttpMultipartFormDataContent();
HttpStreamContent
httpStreamContent = new HttpStreamContent( imageStream );
httpStreamContent.Headers.Add( "Content-Type", "application/octet-stream" );
httpMultipartContent.Add( httpStreamContent, "userAvatar", DateTime.Now.Ticks.ToString( "yyyyMMddhhmmssffff" ) + ".png" );
try {
HttpRequestMessage
httpRequest = new HttpRequestMessage( HttpMethod.Post, new Uri( /* ... */ ) );
Dictionary<String, String>
webCredentialsDictionary = WebServerAuthenticationCredentials( /* ... */ ) as Dictionary<String, String>;
foreach (KeyValuePair<String, String> credentialEntry in webCredentialsDictionary) {
httpRequest.Headers.Add( credentialEntry );
}
httpRequest.Content = httpMultipartContent;
/* The ArgumentException is thrown here. */
HttpResponseMessage
httpResponse = await httpClient.SendRequestAsync( httpRequest );
httpResponse.EnsureSuccessStatusCode();
return new WebResult( true, true, null );
} catch (ArgumentException exception) {
} catch (Exception exception) {
}
/* ... */
}
公共静态异步任务上载映像(WebHost WebHost、对象webPath、字符串webQuery、irandomaccesstream imageStream){
/* ... */
HttpClient
httpClient=new Windows.Web.Http.httpClient();
HttpMultipartFormDataContent
httpMultipartContent=新的HttpMultipartFormDataContent();
HttpStreamContent
httpStreamContent=新的httpStreamContent(imageStream);
httpStreamContent.Headers.Add(“内容类型”、“应用程序/八位字节流”);
添加(httpStreamContent,“userAvatar”,DateTime.Now.Ticks.ToString(“yyyymmddhhmmssfff”)+“.png”);
试一试{
HttpRequestMessage
httpRequest=新的HttpRequestMessage(HttpMethod.Post,新Uri(/*…*/);
字典
webCredentialsDictionary=WebServerAuthenticationCredentials(/*…*/)作为字典;
foreach(网络目录中的KeyValuePair凭证索引){
httpRequest.Headers.Add(credentialEntry);
}
httpRequest.Content=httpMultipartContent;
/*ArgumentException在这里抛出*/
HttpResponseMessage
httpResponse=等待httpClient.SendRequestAsync(httpRequest);
httpResponse.EnsureAccessStatusCode();
返回新的WebResult(true、true、null);
}捕获(ArgumentException异常){
}捕获(异常){
}
/* ... */
}
在执行请求之前,我应该设置什么
此外,这里还有堆栈跟踪:
System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WindowsPhoneFramework.WebController.<UploadImage>d__28.MoveNext()
System.ArgumentException:值不在预期范围内。
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在WindowsPhoneFramework.WebController.d_u28.MoveNext()中
我也有类似的问题。我试图发送一个MemoryStream
转换成IInputStream
request.Content = new HttpStreamContent(ms.AsInputStream());
解决方案是在将流设置为内容之前将流的位置设置为开头。您要将哪些值传递到
httpRequest.Headers.Add()
?您是如何创建流的?添加到httpRequest.Headers
的值是在该服务器上执行请求所必需的凭据。该流来自一个MediaCapture.capturePhototomeryAsync()
。