一个 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);
    }
}

 

转载于:https://www.cnblogs.com/crazybruce/archive/2012/10/22/2734317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值