【问题标题】:C#: How to read data from webbrowser in a windows applicationC#:如何在 Windows 应用程序中从 webbrowser 读取数据
【发布时间】:2008-12-17 07:13:18
【问题描述】:

首先我正在做一个 Windows 应用程序。不是网络应用程序。

现在我正在应用程序将 SMS(短消息)从系统发送到移动设备。

在这里,我使用 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 后,我在浏览器窗口中收到一些文本,例如“消息发送成功”。

我只是想阅读文本并存储在数据库中。

我的问题是:如何从网络浏览器中读取文本。

请抱紧我!

【问题讨论】:

    标签: c# http


    【解决方案1】:

    使用 .NET,请参阅 WebClient Class - 提供向由 URI 标识的资源发送数据和从其接收数据的常用方法。

    在这里见过几次,例如fastest c# code to download a web page

    编辑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 应用程序
    【解决方案2】:

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

    【讨论】:

    • 不需要所有的流废话,只需使用client.DownloadString
    • 感谢 gimel,我忘了说一件事,我正在开发 Windows 应用程序,而不是 Web 应用程序
    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2020-08-08
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多