zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    CSDN博客 专用备份工具
    discuz 7.0 uc 同步登录方法
    delphi 子窗体最大化
    OO系统分析员之路用例分析系列(8)如何编写一份完整的UML需求规格说明书[整理重发]
    delphi 抓取网页内容的程序
    delphi messagebox 使用技巧
    windows mobile下实现程序安装和卸载
    纯真IP库算法
    delphi idhttp 使用方法
    最近评论回复汇总
  • 原文地址:https://www.cnblogs.com/zyosingan/p/1182218.html
Copyright © 2011-2022 走看看