命名空间X已经包含了XX的定义

本文关键字:XX 定义 包含 命名空间 | 更新日期: 2023-09-27 18:19:23

我不明白我得到的这个错误。我曾多次尝试清理和构建我的项目。有人能帮我吗?Program.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.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(website);
                        }
                    }
                }
            }
        }
    }
}

MyWebRequest.cs -问题在这里:公共类MyWebRequest错误是:"命名空间HttpRequestApp已经包含了MyWebRequest的定义"

using HTTPrequestApp.MyWebRequest;
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 : IWebRequest
    {
        public void Request(string strWebSite)
        {
            List<string> lstWebSites = Program.GetList(strWebSite);
            using (var client2 = new TcpClient(strWebSite, 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.IO;
using System.Threading.Tasks;
//using MyWebRequest.Lib;
namespace HTTPrequestApp.MyWebRequest
{
    public interface IWebRequest
    {
        Task<List<strWebSite>> GetList();
        void Request();
    }
}

在这里给出我想要完成的总体视图是:发送HTTP请求以获得初始页面。返回HTTP响应并检查它是否为200响应代码。并计算检索响应所需的时间。这是一个控制台应用程序,但我不需要让它依赖于控制台,它需要是一个独立的应用程序,所以我可以在其他地方使用它。如果有人对我如何简化我的代码有任何建议,请让我知道。谢谢。

命名空间X已经包含了XX的定义

你有一个HTTPrequestApp.MyWebRequest命名空间和一个HTTPrequestApp.MyWebRequest类名:c#编译器混淆了(和人类…)

如果您真的想要一个不同的命名空间,请考虑将命名空间重命名为HTTPrequestApp.MyWebRequestNameSpace