如何异步使用HttpWebRequest(.NET,C#)?
我有一个字符串变量,它包含HTML标记.此HTML标记基本上代表电子邮件内容.
现在我想从这个实际包含HTML标记的字符串内容创建一个图像.我不想通过将这些内容写入其中来创建HTML文件.我只想用这个字符串创建一个图像文件.
这就是我所拥有的:
string emailBody="<html><head></head><body><p>This is my text<p>...</body</html>"
Run Code Online (Sandbox Code Playgroud)
如何从此emailBody字符串内容创建图像?
我正在使用C#HttpWebRequest来获取网页的一些数据.问题是在加载页面后使用javascript/ajax更新了一些数据,而我没有在响应字符串中获取它.有没有办法让webrequest等到页面中的所有脚本都已完成执行?
谢谢
阿米特
我想用它来调用网页上的一些JS脚本.我有这个:
static void Stuff()
{
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.iana.org/domains/example/");
HtmlDocument doc = browser.Document;
//doc.InvokeScript("someScript");
Console.WriteLine(doc.ToString());
}
static void Main(string[] args)
{
Console.WriteLine("hi");
var t = new Thread(Stuff);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
Run Code Online (Sandbox Code Playgroud)
问题1:当我尝试获取时,我得到"对象引用未设置"异常doc.ToString().为什么?
问题2:如何从HTML文档中将一些数据导入主程序?WebBrowser需要一个单独的线程,这需要一个不能返回任何值的静态方法.如何返回,比如说,doc在Main()这样我就可以用它做什么?
我正在使用此方法以编程方式实例化Web浏览器,导航到URL并在文档完成时返回结果.
如果文档加载时间超过5秒,我将如何停止Task并GetFinalUrl()返回null?
我见过许多使用a的例子,TaskFactory但我无法将其应用于此代码.
private Uri GetFinalUrl(PortalMerchant portalMerchant)
{
SetBrowserFeatureControl();
Uri finalUri = null;
if (string.IsNullOrEmpty(portalMerchant.Url))
{
return null;
}
Uri trackingUrl = new Uri(portalMerchant.Url);
var task = MessageLoopWorker.Run(DoWorkAsync, trackingUrl);
task.Wait();
if (!String.IsNullOrEmpty(task.Result.ToString()))
{
return new Uri(task.Result.ToString());
}
else
{
throw new Exception("Parsing Failed");
}
}
// by Noseratio - http://stackoverflow.com/users/1768303/noseratio
static async Task<object> DoWorkAsync(object[] args)
{
_threadCount++;
Console.WriteLine("Thread count:" + _threadCount);
Uri retVal = null;
var wb = new WebBrowser();
wb.ScriptErrorsSuppressed = …Run Code Online (Sandbox Code Playgroud) .net c# console-application webbrowser-control task-parallel-library
我正在建立一个冲浪到几个网站并做一些事情的程序.
在成功冲浪到5个网址之后,该程序在Application.Run()行之后挂起.该程序甚至没有进入处理程序功能,只是卡住了.此时CPU使用率为0.
我尝试以任何可能的方式关闭线程.我做错了什么?
我这样做:
[STAThread]
private static void Main(string[] args)
{
for (int i = 0; i < urls.Count; i++)
{
var th = new Thread(() = >
{
var weBrowser = new WebBrowser();
weBrowser.AllowNavigation = true;
weBrowser.DocumentCompleted += Handler;
weBrowser.Navigate(urls[i]);
Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
}
}
Run Code Online (Sandbox Code Playgroud)
而我的Handle功能是:
private static void Handler(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser weBrowser = sender as WebBrowser;
var htmlDocument = weBrowser.Document;
/*do something*/
Application.Exit();
Application.ExitThread();
weBrowser.Dispose();
weBrowser.Stop();
Thread.CurrentThread.Abort();
}
Run Code Online (Sandbox Code Playgroud)
我的问题非常类似于: Application.Run()导致应用程序挂起
这个问题也没有答案.
谢谢!
我读过的关于这个主题的大多数答案都指向System.Windows.Forms.WebBrowser类或来自Microsoft HTML Object Library程序集的COM接口mshtml.HTMLDocument.
WebBrowser类没有引导我到任何地方.以下代码无法检索由我的Web浏览器呈现的HTML代码:
[STAThread]
public static void Main()
{
WebBrowser wb = new WebBrowser();
wb.Navigate("https://www.google.com/#q=where+am+i");
wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
foreach (IHTMLElement element in doc.all)
{
System.Diagnostics.Debug.WriteLine(element.outerHTML);
}
};
Form f = new Form();
f.Controls.Add(wb);
Application.Run(f);
}
Run Code Online (Sandbox Code Playgroud)
以上只是一个例子.我真的不想找到一个解决方法来找出我所在城镇的名称.我只需要了解如何以编程方式检索那种动态生成的数据.
(调用新的System.Net.WebClient.DownloadString(" https://www.google.com/#q=where+am+i "),将生成的文本保存到某处,搜索您当前所在城镇的名称找到了,如果你能找到它,请告诉我.)
但是,当我从我的网络浏览器(即或Firefox)访问" https://www.google.com/#q=where+am+i "时,我会在网页上看到我的城镇名称.在Firefox中,如果我右键单击城镇名称并选择"Inspect Element(Q)",我会清楚地看到用HTML代码编写的城镇名称,这看起来与WebClient返回的原始HTML完全不同.
在我厌倦了玩System.Net.WebBrowser后,我决定给mshtml.HTMLDocument一个镜头,最后得到同样无用的原始HTML:
public static void Main()
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));
foreach (IHTMLElement e in doc.all)
{
System.Diagnostics.Debug.WriteLine(e.outerHTML);
}
}
Run Code Online (Sandbox Code Playgroud)
我想必须有一种优雅的方式来获取这种信息.现在,我能想到的是将一个WebBrowser控件添加到表单中,让它导航到相关的URL,发送键"CLRL,A",并将页面上显示的任何内容复制到剪贴板并尝试解析它.不过,这是一个可怕的解决方案.
我有一个能够通过WebBrowser对象打印HTML的打印机类.我希望能够从控制台应用程序打印,但是当我的打印机类尝试创建WebBrowser对象时出现错误:
WebBrowser browser = new WebBrowser();
Run Code Online (Sandbox Code Playgroud)
错误:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot
be instantiated because the current thread is not in a
single-threaded apartment.
Run Code Online (Sandbox Code Playgroud)
我尝试将对System.Windows.Forms的引用添加到我的控制台应用程序中,但这不起作用.我对这里发生的事情一无所知,但我很感激你的帮助.
我有来自第三方的Inproc COM服务器.如果它捕获特定类型的错误,我调用的函数之一将显示错误消息对话框.问题是我正在尝试批量处理数据,而我使用的数据源导致错误对话框弹出很多.如果它生成了1000个对话框,这不会成为问题,而是它会阻塞,直到您按OK才会返回该功能.

如何禁止显示对话框,或以编程方式按OK?
这是一个调用堆栈的副本,因为它正在等待我按OK
[Managed to Native Transition]
> System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Line 2198 + 0x1e bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Line 3422 + 0x1b bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3306 + 0xc bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Line 1495 + 0x31 bytes C#
UniversalDataImporter.exe!UniversalDataImporter.Program.Main() Line 18 + 0x1d bytes C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Line 2023 + 0x18 bytes C#
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object … 我正在编写一个消耗资源的库,无论出于什么原因,API的设计方式是在不同的线程上引发事件,但是必须在主线程上完成API的调用.
假设我尝试使用的API被定义为(我将省略事件定义):
public sealed class DodgyService
{
public void MethodThatHasToBeCalledOnTheMainThread() { ... }
}
Run Code Online (Sandbox Code Playgroud)
为了使用这个API,我在我的库中添加了一个名为Service(Yup,非常原始名称)的服务,它将创建一个新任务(当我指定一个已经创建的TaskScheduler时,它将在主线程上运行SynchronizationContext).
这是我的实现:
public class Service
{
private readonly TaskFactory _taskFactory;
private readonly TaskScheduler _mainThreadScheduler;
public Service(TaskFactory taskFactory, TaskScheduler mainThreadScheduler)
{
_taskFactory = taskFactory;
_mainThreadScheduler = mainThreadScheduler;
}
// Assume this method can be called from any thread.
// In this sample is called by the main thread but most of the time
// the caller will be running on a background thread.
public Task …Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×5
html ×2
javascript ×2
.net-4.0 ×1
ajax ×1
asp.net ×1
asynchronous ×1
com ×1
dynamic-html ×1
for-loop ×1
httprequest ×1
messagebox ×1
wpf ×1