【发布时间】:2015-10-27 14:52:11
【问题描述】:
我不明白我遇到的这个错误。我曾多次尝试清理和构建我的项目。谁能帮我? 程序.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 响应代码。以及检索响应所需的时间。 这是一个控制台应用程序,但我不需要它依赖于控制台,它需要是一个独立的应用程序,以便我可以在其他地方使用它。 如果有人对如何简化代码有任何建议,请告诉我。 谢谢。
【问题讨论】:
标签: c# .net http interface request