简体   繁体   English

C#:网络编程 - GET / HTTP 请求的网站

[英]C# : Network Programming - GET / HTTP Requested WEBSITE

So, we have a lab about network programming.所以,我们有一个关于网络编程的实验室。 We ain't gonna go too deep now because this is only an intro, but it's still very tricky.我们现在不会太深入 go 因为这只是一个介绍,但它仍然非常棘手。

Anyway... I am using the localhost (127.0.0.1) as Client and the Server is what we really have to code, as a Console App.无论如何...我使用本地主机(127.0.0.1)作为客户端,而服务器是我们真正需要编写的代码,作为控制台应用程序。

In the directory of the application there are 2 webpages (index.html and secondpage.html) - for testing purposes.在应用程序的目录中有 2 个网页(index.html 和 secondpage.html) - 用于测试目的。

Part 2 of the assignment says:作业的第 2 部分说:

The server checks upon every GET if the requested website exists in the "data system", if it exists , then it sends the following:服务器检查每个 GET 请求的网站是否存在于“数据系统”中,如果存在,则发送以下内容:

  • Confirmation code (200)确认码 (200)
  • Number of chars the webpage has网页的字符数
  • and bla bla bla和bla bla bla

(Basically using FileStream, etc...) (基本上使用FileStream等...)

My question is basically about this "checking"(in bold).我的问题基本上是关于这个“检查”(粗体)。 I can manage the rest, once I get this checking/conditional statement right.一旦我得到这个检查/条件语句,我就可以管理 rest。

Until now that's what I have (snippet of the relevant parts):到目前为止,这就是我所拥有的(相关部分的片段):

private static void ClientHandler(object o)
    {
        TcpClient client = o as TcpClient;
        NetworkStream stream = client.GetStream();

        int i;
        Byte[] bytes = new Byte[256];

        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
        {
            string webPage = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
            Console.WriteLine(webPage);
            
        }

    } 

PS1: ClientHandler is the method that is(as a new thread) executed when the client connects PS1:ClientHandler是客户端连接时(作为新线程)执行的方法

PS2: And that Console.WriteLine was a check to see what exactly I get. PS2:那 Console.WriteLine 是一个检查,看看我到底得到了什么。

Output, using MS Edge, for http://127.0.0.1/index.html ): Output,使用 MS Edge,对于http://127.0.0.1/index.html ):



GET /index.html HTTP/1.1 GET /index.html HTTP/1.1

Host: 127.0.0.1主机:127.0.0.1

Connection: keep-alive连接:保持活动

Cache-Control: max-age=0缓存控制:max-age=0

sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91" sec-ch-ua: "不是;品牌";v="99", "Microsoft Edge";v="91", "Chromium";v="91"

sec-ch-ua-mobile: ?0 sec-ch-ua-mobile: ?0

DNT: 1 Upgrade-Insecure-Requests: 1 DNT:1 升级不安全请求:1

User-Agent: Mozilla/5.0( Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37用户代理:Mozilla/5.0( Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q=0.9接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q=0.9

Sec -Fetch-Site: none Sec-Fetch-Site:无

Sec-Fetch-Mode: navigate Sec-Fetch-Mode:导航

Sec-Fetch-User: ?1 Sec-Fetch-User:?1

Sec-Fetch-Dest: document Sec-Fetch-Dest:文档

Accept-Encoding: gzip, deflate, br接受编码:gzip、deflate、br

Accept-Language: en-GB,en;q=0.9,de;q=0.8,de-DE;q=0.7,en-US;q=0.6接受语言:en-GB,en;q=0.9,de;q=0.8,de-DE;q=0.7,en-US;q=0.6


I was thinking that if I could, for example, create a list of string will all the names of the pages I have, like a "database", get the part of that GET Line that refers to what I wrote after 127.0.0.1, in this case index.html, and then check if the list contains it.我在想,如果我可以,例如,创建一个字符串列表,那么我拥有的所有页面的名称,就像一个“数据库”,得到那个 GET Line 的一部分,它指的是我在 127.0.0.1 之后写的内容,在这种情况下 index.html,然后检查列表是否包含它。

But I don't even get this o/p per line, rather I get the first char of the first line, then the one of the second, till I get the first character of the last line, then it returns, starts getting the second char of the first line, the one of the second, till it gets all of them - in a vertical manner, you know, not in a horizontal one.但我什至没有每行得到这个 o/p,而是我得到第一行的第一个字符,然后是第二个字符,直到我得到最后一行的第一个字符,然后它返回,开始得到第一行的第二个字符,第二个字符,直到它得到所有字符 - 你知道,以垂直方式,而不是水平方式。 Weird.诡异的。 I hope you guys know what I mean.我希望你们知道我的意思。

So, what is this checking and how can I please do it?那么,这是什么检查,我该怎么做呢?

Thanks.谢谢。

A possible solution (or idea) for your problem would be to get the whole request of your client as a string.对于您的问题,一个可能的解决方案(或想法)是将客户的整个请求作为字符串获取。 If it contains linebreaks like your example you would grab the first line and search for the requested document/site page (for example index.html) via Regex.如果它包含像您的示例一样的换行符,您将抓住第一行并通过正则表达式搜索请求的文档/站点页面(例如 index.html)。 You could also apply the regex against the request and fetch the first result.您还可以对请求应用正则表达式并获取第一个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM