C#WebClient OpenRead网址

C#WebClient OpenRead网址,第1张

概述所以我有这个程序使用短链接获取页面(我使用谷歌网址缩短器). 为了构建我的示例,我使用了 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("@R_403_6822@://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(“@R_403_6822@://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}
总结

以上是内存溢出为你收集整理的C#WebClient OpenRead网址全部内容,希望文章能够帮你解决C#WebClient OpenRead网址所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1219118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存