添加接口

本文关键字:接口 添加 | 更新日期: 2023-09-27 18:19:23

我的主要程序。cs如下:

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var lstWebSites = new List<string>
            {
                "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();
                        }
                    }
                }
            }
        }
    }
}

然后我有一个类,这就是错误所在。

GetList()错误- ` HTTPrequestApp. `程序'不包含'GetList'的定义

client2错误-名称'client2'在当前内容中不存在

MyWebRequest.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
    public class MyWebRequest : HTTPrequestApp.IWebRequest
    {
        public void Request()
        {
            List<string> lstWebSites = Program.GetList();
            using (var client = new TcpClient(lstWebSites[1], 80))
            {
                using (NetworkStream stream = client2.GetStream())
                using (StreamWriter writer = new StreamWriter(stream))
                using (StreamReader reader2 = new StreamReader(stream))
                {
                    writer.AutoFlush = true;
                    writer.WriteLine("GET / HTTP/1.1");
                    writer.WriteLine("HOST: {0}:80", lstWebSites[1]);
                    writer.WriteLine("Connection: Close");
                    writer.WriteLine();
                    writer.WriteLine();
                    string theresponse = reader2.ReadToEnd();
                    Console.WriteLine(theresponse);
                }
            }
        }
    }
}

最后,我有一个接口。这样做正确吗?

如果我做错了什么,请帮助我,我该如何修复它?

IWebRequest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HTTPrequestApp
{
    interface IWebRequest
    {
        void Request();
    }
}

我要做的是:发送HTTP请求以获得初始页面并获得HTTP响应。将其保存到.cvs文件中。检查它是否为200响应代码,并计算检索响应所需的时间。我必须从我列表中的每个网站得到4次回应。请帮帮我。

添加接口

首先说明你的错误:

  1. 提供的代码不包含GetList方法的程序类,因为代码共享包含唯一的主方法来定义你的网站。
  2. using (var client = new TcpClient(lstWebSites[1], 80))行创建客户端对象而不是client2。

另一点,而不是写打开TCPClient连接来读取网站的响应,你可以使用HttpClientWebRequest内置类来实现你的功能。

下面是一个完整的例子。继续添加您想要的所有网站到lstWebSites,而不是将HTML结果转储到控制台,您可以将它们写入一个文件。

var lstWebSites = new List<string>
        {
            "https://www.stackoverflow.com",
            "https://www.google.com"
        };
foreach (string website in lstWebSites)
{
    var request = WebRequest.Create(website);
    request.Credentials = CredentialCache.DefaultCredentials;
    ((HttpWebRequest)request).UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";   // Lie
    var response = request.GetResponse();
    if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
    {
        var stream = response.GetResponseStream();
        var reader = new StreamReader(stream);
        Console.WriteLine(string.Format("***** {0} *****", website));
        Console.WriteLine(reader.ReadToEnd());  // Dump HTML response
    }
}