c# - HTTP请求获取初始页面

本文关键字:获取 HTTP 请求 | 更新日期: 2023-09-27 18:19:11

我必须发送HTTP请求来获取初始页面,然后获取HTTP响应并检查是否为200响应代码。所有这些都必须保存为。csv文件,每个网站保存四次。这是我得到的结果:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var lstWebSites = new List<string>
            {
                "www.mearstransportation.com",
                "www.amazon.com",
                "www.ebay.com",
                "www.att.com",
                "www.verizon.com",
                "www.sprint.com",
                "www.centurylink.com",
                "www.yahoo.com"
            };
            string filename = @"RequestLog.txt";
            {
                using (var writer = new StreamWriter(filename, true))
                {
                    foreach (string website in lstWebSites)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            MyWebRequest request = new MyWebRequest();
                            request.Request();
                        }
                    }
                }
            }
        }
    }
}

我仍然需要在我创建的另一个类MyWebRequest.cs中做GET请求

c# - HTTP请求获取初始页面

在MyWebRequest类中执行以下操作(您还需要将网站url字符串传递给MyWebRequest,但我只是显式地这样做):

    HttpWebResponse response = null;
    try
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com/thisisadeadlink");
        request.Method = "GET";
        response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        Console.Write(sr.ReadToEnd());
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError)
        {
            response = (HttpWebResponse)e.Response;
            Console.Write("Errorcode: {0}", (int)response.StatusCode);
        }
        else
        {
            Console.Write("Error: {0}", e.Status);
        }
    }
    finally
    {
        if (response != null)
        {
            response.Close();
        }
    }

如果服务器失败和响应,这应该捕获。StatusCode不为200

由于您的MyWebRequest使用HttpWebRequest, Request方法可以返回HttpWebResponse,如果它有URL作为如下参数:

public class MyWebRequest 
{
    public HttpWebResponse Request(string url)
    {
        HttpWebResponse response = null;
        try
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            response = (HttpWebResponse)httpWebRequest.GetResponse();
        }
        catch (WebException ex)
        {
            // Handle exception
        }
        return response;
    }
}

,它可以在for循环中这样调用:

HttpWebResponse response = request.Request("http://" + website);
if ((response != null) && (response.StatusCode == HttpStatusCode.OK))
{
    // Write .csv file
}