をPUT例外が発生します:C#のHttpClient何らかの理由で結果的に今働いていた私の以下のコードを
public static async Task<string> HttpPut(string inUrl, string inFilePath)
{
using (var handler = new HttpClientHandler
{
AllowAutoRedirect = false
})
{
using (var client = new HttpClient(handler))
{
//var content = new StreamContent(new FileStream(inFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true));
using (var content = new StreamContent(new FileStream(inFilePath, FileMode.Open)))
{
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "application/octet-stream");
using (var req = new HttpRequestMessage(HttpMethod.Put, inUrl))
{
string authInfo = String.Format("{0}:{1}", Program.Config.MediaStorageList.Find(o => o.Name == "Viz Media Engine Test").UserName, Program.Config.MediaStorageList.Find(o => o.Name == "Viz Media Engine Test").Password);
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization", "Basic " + authInfo);
req.Headers.Remove("Expect");
req.Headers.Add("Expect", "");
//req.Headers.TransferEncodingChunked = true;
req.Content = content;
// Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
using (HttpResponseMessage resp = await client.SendAsync(req))
{
//This part is specific to the setup on an Expo we're at...
if (resp.StatusCode == HttpStatusCode.Redirect || resp.StatusCode == HttpStatusCode.TemporaryRedirect)
{
string redirectUrl = resp.Headers.Location.ToString();
if (redirectUrl.Contains("vme-store"))
{
redirectUrl = redirectUrl.Replace("vme-store", "10.230.0.11");
}
return await HttpPut(redirectUrl, inFilePath);
}
resp.EnsureSuccessStatusCode();
return await resp.Content.ReadAsStringAsync();
}
}
}
}
}
}
私は取得しています例外がある:
System.NotSupportedException was unhandled
HResult=-2146233067
Message=The stream does not support concurrent IO read or write operations.
Source=System
StackTrace:
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult ar)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at VizWolfInnerServer.Tools.HttpConnector.<HttpPut>d__39.MoveNext() in c:\Users\christer\Documents\Visual Studio 2012\Projects\VizWolfNew\VizWolfInnerServer\Tools\HttpConnector.cs:line 202
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at VizWolfInnerServer.Tools.VizAPIConnector.<VmeUploadMedia>d__0.MoveNext() in c:\Users\christer\Documents\Visual Studio 2012\Projects\VizWolfNew\VizWolfInnerServer\Tools\VizAPIConnector.cs:line 187
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
InnerException:
、私は非常に抱えていますHttpClientの適切なドキュメントとサンプルを見つけるのは苦労します。なぜこれが突然動作しないのか(ストリームコンテンツの代わりにStringContentと全く同じメソッドが完全に動作します)を理解するのに苦労しています...
もともとはそれ自身のスレッドから呼び出され、その後、次のように:
public static async void VmeUploadMedia(string inUploadLink, string inFilePath)
{
string result = await HttpConnector.HttpPut(inUploadLink, inFilePath);
}
誰もが明らかに何かを発見しますか?
おかげ
UPDATE
は、博覧会、みんながそれを自分のストレージ名をマップするために得ることが判明し、私は最善の解決策だった戻って私の元のコードに行くことができるようにIPです。私が持っていた問題はAllowAutoRedirect = falseと関係しています。実際にリダイレクトが行われていない場合でも、HttpResponseMessage resp = clientAttendTime(req)で例外が発生しました。私は種類のそれも起こっていた理由を失ったんだけど、このコードを使用すると、すべてが今取り組んでいる:
public static async Task<string> HttpPut(string inUrl, string inFilePath)
{
using (var client = new HttpClient())
{
using (var content = new StreamContent(File.OpenRead(inFilePath)))
{
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "application/octet-stream");
using (var req = new HttpRequestMessage(HttpMethod.Put, inUrl))
{
string authInfo = String.Format("{0}:{1}", Program.Config.MediaStorageList.Find(o => o.Name == "Viz Media Engine Test").UserName, Program.Config.MediaStorageList.Find(o => o.Name == "Viz Media Engine Test").Password);
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization", "Basic " + authInfo);
req.Headers.Remove("Expect");
req.Headers.Add("Expect", "");
req.Content = content;
// Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
using (HttpResponseMessage resp = await client.SendAsync(req))
{
resp.EnsureSuccessStatusCode();
return await resp.Content.ReadAsStringAsync();
}
}
}
}
}
あなたがHttpPut()
を呼び出しているように見えます
を手助けしようとしていた人々に
感謝をサイト上の実際の回答と正しい答えとして選択します。現時点では未回答の質問のようです。 – dmportella
良い点、今やって – CeeRo
あなたは今、PutAsJsonAsync <>を使用していませんか? – niico