• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
布鲁斯
Your mind breaks the spirit of your soul.
博客园    首页    新随笔    联系   管理    订阅  订阅

一个 C# .NET 2.0 HTTP GET 方式访问类

/*
 *    HTTPGet.cs | C# .NET 2.0 HTTP GET Class
 *    Copyright (c) 2008, Corey Goldberg
 *
 *    HTTPGet.cs is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 */


using System;
using System.IO;
using System.Net;
using System.Text;
using System.Diagnostics;



public class HTTPGet
{
    private HttpWebRequest request;
    private HttpWebResponse response; 
    
    private string responseBody;
    private string escapedBody;
    private int statusCode;
    private double responseTime;

    public string ResponseBody {get { return responseBody; }}
    public string EscapedBody {get { return GetEscapedBody(); }}
    public int StatusCode {get { return statusCode; }}
    public double ResponseTime {get { return responseTime; }}
    public string Headers {get { return GetHeaders(); }}
    public string StatusLine {get { return GetStatusLine(); }}



    public void Request(string url)
    {
        Stopwatch timer = new Stopwatch();
        StringBuilder respBody = new StringBuilder();

        this.request = (HttpWebRequest)WebRequest.Create(url);

        try
        {
            timer.Start(); 
            this.response = (HttpWebResponse)this.request.GetResponse();
            byte[] buf = new byte[8192];
            Stream respStream = this.response.GetResponseStream();
            int count = 0;
            do
            {
                count = respStream.Read(buf, 0, buf.Length);
                if (count != 0)
                    respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
            }
            while (count > 0);
            timer.Stop();

            this.responseBody = respBody.ToString();
            this.statusCode = (int)(HttpStatusCode)this.response.StatusCode;
            this.responseTime = timer.ElapsedMilliseconds / 1000.0;
        }
        catch (WebException ex)
        {
            this.response = (HttpWebResponse)ex.Response;
            this.responseBody = "No Server Response";
            this.escapedBody = "No Server Response";
            this.responseTime = 0.0;
        }
    }



    private string GetEscapedBody()
    {  // HTML escaped chars
        string escapedBody = responseBody;
        escapedBody = escapedBody.Replace("&", "&");
        escapedBody = escapedBody.Replace("<", "<");
        escapedBody = escapedBody.Replace(">", ">");
        escapedBody = escapedBody.Replace("'", "'");
        escapedBody = escapedBody.Replace("\"", """);
        this.escapedBody = escapedBody;

        return escapedBody;
    }



    private string GetHeaders()
    {
        if (response == null)
            return "No Server Response";
        else
        {
            StringBuilder headers = new StringBuilder();
            for (int i = 0; i < this.response.Headers.Count; ++i)
                headers.Append(String.Format("{0}: {1}\n",
                    response.Headers.Keys[i], response.Headers[i]));

            return headers.ToString();
        }
    }



    private string GetStatusLine()
    {
        if (response == null)
            return "No Server Response";
        else
            return String.Format("HTTP/{0} {1} {2}", response.ProtocolVersion, 
                (int)response.StatusCode, response.StatusDescription);
    }
}

 调用

public class Program
{
    static void Main(string[] args)
    {
        HTTPGet req = new HTTPGet();
        req.Request("http://www.google.com");
        Console.WriteLine(req.StatusLine);
        Console.WriteLine(req.ResponseTime);
    }
}

 

posted @ 2012-10-22 17:55  布鲁斯  阅读(4029)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3