C#:如何在Windows应用程序中从WebBrowser读取数据

4
首先,我正在开发一个Windows应用程序,而不是Web应用程序。
现在,我正在开发一个从系统发送短信(短信)到手机的应用程序。
在这里,我使用一个HTTP URL来推送消息,其中包含参数To(号码)和Msg(测试消息)。
形成URL后,如下所示: http://333.33.33.33:3333/csms/PushURL.cgi?USERNAME=xxxx&PASSWORD=xxxx&MOBILENO=919962391144&MESSAGE=TestMessage&TYPE=0&CONTENT_TYPE=text
这里我提到了3个IP地址,X代表密码和用户ID,因为这是机密信息。
发送此URL后,在浏览器窗口中会收到一些文本,例如“消息发送成功”。
我想读取文本并将其存储在数据库中。
我的问题是:我该如何从Web浏览器中读取文本。
请帮助我!
2个回答

2
使用.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 ();
}

}


谢谢Gimel,我忘了说一件事,我正在开发一个Windows应用程序,而不是Web应用程序。 - rajesh

0

这是来自微软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 ();
    }
}

1
不要使用那些流的复杂操作,直接使用 client.DownloadString 即可。 - John Sheehan
谢谢Gimel,我忘了说一件事,我正在开发一个Windows应用程序,而不是Web应用程序。 - rajesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接