我主要的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.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.Program”不包含“的GetList”
的客户机程序错误的定义 - 这个名字“客户端2”在目前的内容
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次。 请帮帮我。
除非您希望从头开始编写HTTP编程,否则我建议使用其中一个本机类。使用WebRequest的http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/,你也许可以做你的几行代码想要什么https://msdn.microsoft.com/en-我们/库/ 456dfw4f(v = vs.110)的.aspx –
谢谢,这说明以简单的方式一切...... – NewDev
当我试图加入MyWebRequest正如我上面提到我收到同样的错误回报的Web客户端,我有使用引用,并检查System.dll的全部。有什么建议么?另外,我是否正确添加了我的界面? – NewDev