.net VSTO Outlook插件- HttpClient.PostAsync在没有fiddler的情况下失败

gupuwyp2  于 2023-04-13  发布在  .NET
关注(0)|答案(4)|浏览(234)

不幸的是,我在插件中开发这个功能的整个过程中一直在运行Fiddler,自从部署到客户端后,我发现它对任何人都不起作用--除非他们也运行Fiddler!如果我停止运行Fiddler,它在我的开发机器上也不起作用。
主要错误是Error while copying content to a stream.。(对我来说,这解释了为什么运行Fiddler解决了这个问题-因为我相信请求会首先发送给Fiddler,然后Fiddler将其发送到服务器)。然而,我找不到任何东西来支持这可能是问题所在。我已经尝试确保它保存数据,但我没有。我觉得我走的路不对。
代码大致如下:

HttpClientHandler httpHandler = new HttpClientHandler { UseDefaultCredentials = true };
var client = new HttpClient(httpHandler, false);
client.BaseAddress = new Uri(BaseUrl + "api/job/PostTest");

var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture));

content.Add(new StringContent(mailItem.HTMLBody, Encoding.UTF8), "BodyHtml");
// content.Add()'s... Omitted for brevity

var response = client.PostAsync(BaseUrl + "api/job/PostTest", content);

response.ContinueWith(prevTask => {

    if (prevTask.Result.IsSuccessStatusCode)
    {
        System.Diagnostics.Debug.WriteLine("Was success");
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("Was Error");
    }

}, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion);


response.ContinueWith(prevTask =>{
    MessageBox.Show(prevTask.Exception.ToString());
}, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted);

完整的例外详细信息为:

System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.Net.WebException: The request was aborted: The request was canceled.
   at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
   at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   at System.Net.Http.StreamToStreamCopy.StartRead()
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.Net.WebException: The request was aborted: The request was canceled.
   at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
   at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
   at System.Net.Http.StreamToStreamCopy.StartRead()
   --- End of inner exception stack trace ---<---

如果有人能给我指出一些对我有帮助的资源,或者指出我哪里出错了,那将有很大的帮助!

uelo1irk

uelo1irk1#

在开发我们的IronBox Outlook插件时,我们遇到了这个问题。我们发现在VSTO上下文中,ServicePointManager支持的安全协议只有TLS和Ssl3(这不适用于我们的API,它只支持TLS 1.2或更高版本)。
你可以在你的VSTO代码中很容易地检查这个,就像这样(这里有一个例子,当我们钩住Application.ItemSend事件时):

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    // Handle event when item is sent
    this.Application.ItemSend += Application_ItemSend;        
}

private void Application_ItemSend(object Item, ref bool Cancel)
{
    foreach (var c in (SecurityProtocolType[])Enum.GetValues(typeof(SecurityProtocolType)))
    {
        if (ServicePointManager.SecurityProtocol.HasFlag(c))
        {
            Debug.WriteLine(c.ToString());
        }
    }
    Cancel = false;
}

我们通过设置ServicePointManager.SecurityProtocol属性来支持Tls12解决了这个问题,如下所示:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

希望有一天这能帮助到别人,
凯文

fkvaft9z

fkvaft9z2#

经过大量的搜索和混乱,我无法使用HttpClient解决这个问题,所以我所做的是使用WebClient。如果其他人在未来遇到这个问题,我将发布我最终使用的内容:

System.Net.WebClient wc = new System.Net.WebClient();
wc.Headers.Add("Content-Type", String.Format("multipart/form-data; boundary=\"{0}\"", multipartFormBoundary));
wc.UseDefaultCredentials = true;

try
{
    var wcResponse = wc.UploadData(BaseUrl + "api/job/PostJobEmailNote", byteArray);
}
catch(Exception e)
{
    // response status code was not in 200's 
}
h79rfbju

h79rfbju3#

我想问题可能是使用返回void的匿名函数。它们有点问题。将我的lambda更改为返回bools的函数为我解决了这个问题。

e0bqpujr

e0bqpujr4#

您没有在等待async Http调用。请尝试使用await

var response = await client.PostAsync(BaseUrl + "api/job/PostTest", content);

相关问题