65.9K
CodeProject 正在变化。 阅读更多。
Home

HttpWebRequest

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

1分钟阅读

viewsIcon

22126

HttpWebRequest 类允许您以编程方式向 HTTP 服务器发出 Web 请求。 此代码演示如何读取

HttpWebRequest

HttpWebRequest 类允许您以编程方式向 HTTP 服务器发出 Web 请求。

此代码演示如何使用 HttpWebRequest 类从远程 Web 服务器读取文件的内容。

Visual Basic

如果 不是 (IsPostBack) 那么

    试试

        Dim fr As System.Net.HttpWebRequest

        Dim targetURI As New Uri("http://weblogs.asp.net/farazshahkhan")

 

        fr = DirectCast(System.Net.HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)

        '在上面的代码中,http://weblogs.asp.net/farazshahkhan 仅用作示例

        '它可以是具有不同文件名和扩展名的不同域名

        如果 (fr.GetResponse().ContentLength > 0) 那么

            Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream())

            Response.Write(str.ReadToEnd())

            str.Close() 
        结束 如果

 

    捕获 ex As System.Net.WebException

        Response.Write("文件不存在。")

    结束 尝试

结束 如果

C#

if (!(IsPostBack))

{

    try

    {

        System.Net.HttpWebRequest fr;

        Uri targetUri = new Uri("http://weblogs.asp.net/farazshahkhan");

        fr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(targetUri);

        //在上面的代码中,http://weblogs.asp.net/farazshahkhan 仅用作示例

        //它可以是具有不同文件名和扩展名的不同域名

        if ((fr.GetResponse().ContentLength > 0))

        {

            System.IO.StreamReader str = new System.IO.StreamReader(fr.GetResponse().GetResponseStream());

            Response.Write(str.ReadToEnd());

            if (str != null) str.Close();

        }

    }

    catch (System.Net.WebException ex)

    {

        Response.Write("文件不存在。");

    }




}

  

链接

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

 

© . All rights reserved.