2015-10-26 49 views
0

我主要的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次。 请帮帮我。

+0

除非您希望从头开始编写HTTP编程,否则我建议使用其中一个本机类。使用WebRequest的http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/,你也许可以做你的几行代码想要什么https://msdn.microsoft.com/en-我们/库/ 456dfw4f(v = vs.110)的.aspx –

+0

谢谢,这说明以简单的方式一切...... – NewDev

+0

当我试图加入MyWebRequest正如我上面提到我收到同样的错误回报的Web客户端,我有使用引用,并检查System.dll的全部。有什么建议么?另外,我是否正确添加了我的界面? – NewDev

回答

1

首先对你的错误:

  1. 所提供的代码不包含在程序类的GetList方法共享代码包含定义您的网站的唯一主要方法。
  2. 线using (var client = new TcpClient(lstWebSites[1], 80))制作客户对象,而不是客户端2。

还有一点,而不是写打开的TcpClient连接阅读网站的响应,你可以使用内置类HttpClientWebRequest实现自己的功能。

0

这里是我的意思一个完整的例子。继续添加您想要的所有网站到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 
    } 
} 
+0

这看起来不错,我试图用这个代码,但Console.WriteLine($“***** {}网站*****”}是给我一个)预期的错误。 – NewDev

+0

立即尝试。我使用了更现代的字符串格式语法。 –