HttpClient通过PostAsync上传文件的奇怪行为(Weird behavior of HttpClient uploading file via PostAsync)
我希望有人对此有解释,因为它让我疯了。
我在WinForms应用程序中有以下代码,用于将使用HttpClient的文件上载到Web API服务。
ProgressMessageHandler progressMsgHandler = new ProgressMessageHandler(); progressMsgHandler.HttpSendProgress += (s, e) => { this.InvokeIfNeeded(() => { // LOG this.AddLogLine("Progress: " + e.ProgressPercentage.ToString()); }); }; using (var client = HttpClientFactory.Create(progressMsgHandler)) { using (var content = new MultipartFormDataContent()) { var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); var streamContent = new StreamContent(stream); content.Add(streamContent); var url = String.Format("{0}?src={1}", STR_URL_FILEMGMT, "IM"); client.PostAsync(new Uri(url), content).ContinueWith(task => { if (task.Result.IsSuccessStatusCode) { this.InvokeIfNeeded(() => { // LOG this.AddLogLine("File has been uploaded OK!"); }); } else this.InvokeIfNeeded(() => { // LOG this.AddLogLine(String.Format("Error during upload: '{0}'", task.Exception.Message)); }); }); } }
请注意,列出的整个代码也包含在try catch中,如果有的话,将打印异常。
对我来说很奇怪的是我根本没有打印任何日志行。 既不是为了进步,也不是为了成功发布,既没有发布错误也没有发布全局尝试。
在调试期间,我看到代码正在执行,但没有遇到日志行所在的任何断点。
可能我没有正确使用HttpClient或这些任务(我是初学者在这btw)? 有谁知道我的代码出了什么问题?
更新 (从Noseratio获得解决方案后):
可以在此处找到有助于我了解HttpClient不需要处理的额外信息
I hope someone has an explanation to this, because it drives me nuts.
I have the following code in a WinForms application to upload a file using HttpClient to a Web API service.
ProgressMessageHandler progressMsgHandler = new ProgressMessageHandler(); progressMsgHandler.HttpSendProgress += (s, e) => { this.InvokeIfNeeded(() => { // LOG this.AddLogLine("Progress: " + e.ProgressPercentage.ToString()); }); }; using (var client = HttpClientFactory.Create(progressMsgHandler)) { using (var content = new MultipartFormDataContent()) { var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); var streamContent = new StreamContent(stream); content.Add(streamContent); var url = String.Format("{0}?src={1}", STR_URL_FILEMGMT, "IM"); client.PostAsync(new Uri(url), content).ContinueWith(task => { if (task.Result.IsSuccessStatusCode) { this.InvokeIfNeeded(() => { // LOG this.AddLogLine("File has been uploaded OK!"); }); } else this.InvokeIfNeeded(() => { // LOG this.AddLogLine(String.Format("Error during upload: '{0}'", task.Exception.Message)); }); }); } }
Please note, entire code listed is also wrapped into a try catch where exception would be printed, if any.
Very weird to me is that I don't get any log line printed at all. Neither for progress, neither for posting with success, neither for posting with errors nor in the global try catch.
During debug, I see the code executing, but doesn't hit any breakpoints where the log lines are.
Probably I don't use correctly HttpClient or these tasks (I am beginner at this btw)? Does anyone know what's going wrong with my code?
UPDATE (after getting the solution from Noseratio):
Extra information which helps me understand that HttpClient does not need to be disposed can be found here
原文:https://stackoverflow.com/questions/22211401
最满意答案
尝试
self requestContext request uri
如果你不是一个组件,你可以做任何对象
WACurrentRequestContext value request uri
Try
self requestContext request uri
and if you are not in a component but any object you can do
WACurrentRequestContext value request uri
相关问答
更多-
我有问题1和2的答案: 这是真的。 然而,自2.8版以来,Seaside不再是一个严格的“基于连续性”的框架。 Seaside仅在流量模块中使用延续。 由于Seaside 3.0流量模块甚至是可选的。 另外请注意,自2005年以来,Seaside一直对Javascript提供强大的支持,这是在主流框架开始添加Javascript功能之前很久。 今天Seaside带有内置的JQuery和JQueryUI支持。 当然,这取决于你存储在会话对象中的内容,但通常会话很小(小于20 KiB)。 使用应用程序中的内存分 ...
-
如何在Pharo 4.0中加载Magritte3和Seaside 3.0?(How do I load Magritte3 and Seaside 3.0 in Pharo 4.0?)[2022-05-18]
在配置浏览器中,而不是安装稳定版本,请选择“ 加载配置”按钮。 然后打开游乐场(世界>游乐场)并执行以下操作 (ConfigurationOfSeaside3 project version: #'release3.0') load 如果您需要不同的版本,请打开ConfigurationOfSeaside3类(您可以通过选择名称并按ctrl + b或通过Spotter(shift + enter)来执行此操作。 打开后,选择symbolic versions协议并查看可用的版本。 您也可以以相同的方式加载 ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
要序列化事件的x鼠标坐标,请使用以下代码: html div onClick: (html jQuery ajax callback: [ :x | x inspect ] value: JQEvent new pageX); with: 'Click'. 事件对象中还有其他属性,您可能会感兴趣,只需通过添加多个callback:value:函数callback:value: constructs in a cascade,将它们序列化为相同的AJAX请求。 ...
-
结果我在初始化方法中没有super initialize 。 现在一切都很好; 随意禁用/删除/但是这个线程! :) Turns out I didn't have super initialize in my initialize method. All is good now; feel free to disable/delete/howeveritworks this thread! :)
-
下面的代码应该这样做: WAApplication allInstances do: [ :application | application keysAndHandlersDo: [ :key :session | | policy table | policy := application cache expiryPolicy. table := policy instVarNamed: 'lastAccessTable'. Transcript ...
-
尝试alt+. (Linux)或cmd+. (OS X)。 这应该触发用户中断并打开调试器(您可能需要连续几次按键组合键)。 较旧的Seaside版本也曾用于通过/seaside/config公开的Web界面暂停和恢复UI过程。 不确定是否仍然可以。 Try alt+. (Linux) or cmd+. (OS X). That should trigger a user interrupt and open a debugger (you'll possibly need to hit the key c ...
-
据我所知,没有简单的方法可以做到这一点。 非常简化,并且忽略了Seaside所采取的许多优化,Seaside会话是等待恢复的执行堆栈的快照。 将该冻结的执行堆栈迁移到另一个映像并重新启动它并非易事。 As far as I know there is no easy way to do it. Very simplified, and ignoring many optimizations taken by Seaside, Seaside session is snapshot of execution ...
-
尝试 self requestContext request uri 如果你不是一个组件,你可以做任何对象 WACurrentRequestContext value request uri Try self requestContext request uri and if you are not in a component but any object you can do WACurrentRequestContext value request uri
-
builder.seaside.st应该为您提供任何可能配置的工作加载脚本。 确保检查jQuery和jQueryUI ,然后单击Load Script下载一个脚本,您可以将其拖放到Pharo Dev Image上 。 注意,jQuery不适用于Seaside 2.8。 builder.seaside.st should give you a working load script for any possible configuration. Make sure to check jQuery and jQ ...