加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#WebClient OpenRead网址

发布时间:2020-12-16 10:18:03 所属栏目:百科 来源:网络整理
导读:所以我有这个程序使用短链接获取页面(我使用谷歌网址缩短器). 为了构建我的示例,我使用了 Using WebClient in C# is there a way to get the URL of a site after being redirected?的代码 using System;using System.Collections.Generic;using System.Linq
所以我有这个程序使用短链接获取页面(我使用谷歌网址缩短器).
为了构建我的示例,我使用了 Using WebClient in C# is there a way to get the URL of a site after being redirected?的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyWebClient client = new MyWebClient();
            client.OpenRead("http://tinyurl.com/345yj7x");            
            Uri uri = client.ResponseUri;            
            Console.WriteLine(uri.AbsoluteUri);
            Console.Read();
        }
    }

    class MyWebClient : WebClient
    {
        Uri _responseUri;

        public Uri ResponseUri
        {
            get { return _responseUri; }
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            _responseUri = response.ResponseUri;
            return response;
        }
    }
}

我不明白一件事:当我做客户时.OpenRead(“http://tinyurl.com/345yj7x”);这会下载网址指向的页面吗?如果这个方法下载页面,我需要一些东西才能得到我的网址,所以如果有方法只获取一些标题,或只有网址,请告诉我.

解决方法

您只能使用HEAD请求获取标头,如下所示:

var request = WebRequest.Create(sourceUri);
request.Method = "HEAD";

var response = request.GetResponse();
if (response != null) {
    // You can now use response.Headers to get header info
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读