![](/img/trans.png)
[英]How to open a URL in new window using WebBrowser control in c# Windows Forms application and read its header information (via regex etc)
[英]C#: How to read data from webbrowser in a windows application
首先,我正在做Windows應用程序。 不是網絡應用程序。
現在,我正在做從系統向手機發送SMS(短消息)的應用程序。
在這里,我正在使用http URL推送具有參數To(數字)和Msg(測試消息)的消息。
形成網址后,例如
在這里,由於機密性,我提到的IP地址為3,密碼和用戶ID為X。
發送此URL后,我在瀏覽器窗口中收到諸如“消息發送成功”之類的文本。
我只是想閱讀文本並存儲在數據庫中。
我的問題是:如何從網絡瀏覽器讀取文本。
請抱住我!
使用.NET,請參閱WebClient類 -提供用於向URI標識的資源發送數據和從中接收數據的通用方法。
幾次出現在這里,例如最快的C#代碼下載網頁
編輯 : System.Net.WebClient
類未連接到Web應用程序,並且可以輕松地在控制台或Winforms應用程序中使用。 MSDN鏈接中的C#示例是一個獨立的控制台應用程序(編譯並運行它進行檢查):
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main (string[] args)
{
if (args == null || args.Length == 0)
{
throw new ApplicationException ("Specify the URI of the resource to retrieve.");
}
WebClient client = new WebClient ();
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
}
}
這是來自Microsoft WebClient Class的代碼。
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main (string[] args)
{
if (args == null || args.Length == 0)
{
throw new ApplicationException ("Specify the URI of the resource to retrieve.");
}
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.