• webrequest请求远程页面


    /// <summary>
    /// 执行请求的连接,获取远程资源,返回String
    /// </summary>
    /// <param name="url"> 远程资源连接 </param>
    /// <param name="encode"> 编码格式(Default = "") </param>
    /// <returns> 返回获取的字符串 </returns>
    public static string httpWebResponseString(string url, string encode)
    {
    string str = "";
    try
    {
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    if (httpRequest.HaveResponse)
    {
    StreamReader sr;
    if (encode != null && encode != "")
    {
    sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding(encode));
    }
    else
    {
    sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.Default);
    }


    str = sr.ReadToEnd();
    sr.Close();
    httpResponse.Close();
    }
    }
    catch (Exception ex)
    {
    ILog.SystemLog(url " ¦*** ¦" ex.Message);
    }
    return str;
    }

    或者

    /// <summary>
    /// 执行请求的连接,获取远程资源,返回String
    /// </summary>
    /// <param name="url"> 远程资源连接 </param>
    /// <param name="encode"> 编码格式(Default = "") </param>
    /// <returns> 返回获取的字符串 </returns>
    public static string httpWebResponseString(string url, string encode)
    {
    string str = "";
    try
    {
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    if (httpRequest.HaveResponse)
    {
    StreamReader sr;
    if (encode != null && encode != "")
    {
    sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding(encode));
    }
    else
    {
    sr = new StreamReader(httpResponse.GetResponseStream(), Encoding.Default);
    }
    str = sr.ReadToEnd();
    sr.Close();
  • 相关阅读:
    生产者消费者模型
    varchar2存储汉字,英文字符,数字在oracle中的多少
    正则表达式以及邮箱
    爬虫
    创建git本地仓库和GitHub远程仓库并配置连接的从无到有
    CSSHTML实现高度宽度自适应
    实现一个元素在当前窗口垂直水平居中的几种方法
    angular4 rxjs 异步处理多个http请求数据
    angular 4 父子组件异步交互
    同步异步单线程多线程初级理解
  • 原文地址:https://www.cnblogs.com/zyosingan/p/1182218.html
Copyright © 2020-2023  润新知