C#如何通过用于HTTP的TCP发送文件?

时间:2019-05-14 13:47:09

标签: c# tcplistener

我目前面临的问题是我无法让C#程序编写要在响应中发送的文件(我正在写文件的字节),并且没有有效的HTTP响应,如果我已将文件附加到响应中,则用于测试它的浏览器会显示“ ERR_INVALID_HTTP_RESPONSE”。

最初我使用的是HttpListener,但这要求我每次都以admin身份运行IDE,因此我决定离开它并开始使用TcpListener。

这只是一个简单的Web服务器,完全使用C#运行,并使用System.Net中的TcpListener。到目前为止,我已经尝试使用StreamWriter中的BaseStream将文件的字节写入响应中。

我一直在寻找其他通过TCP / HTTP发送文件的方法,但是似乎有两件事很明显,它没有关于如何正确执行文件的详尽记录,似乎没有人问过相同的问题具体情况。

到目前为止,处理对传入连接的响应的代码如下。

      StreamReader sr = new StreamReader(client.GetStream());
      StreamWriter sw = new StreamWriter(client.GetStream());
      string request = sr.ReadLine();
      if (request != null) {
        //byte[] testData = Encoding.UTF8.GetBytes("Test");
        string[] tokens = request.Split(" ");
        try {
          FileInfo file = files[tokens[1]];
          byte[] fileBytes = File.ReadAllBytes(file.FullName);
          sw.WriteLine("HTTP/1.0 200 OK\n");
          sw.BaseStream.Write(fileBytes, 0, fileBytes.Length);
          sw.Flush();
        } catch {
          sw.WriteLine("HTTP/1.0 404 OK\n");
          sw.Write("<h1>Error 404</h1><p>The file you were looking for does not exist</p>");
          sw.Flush();
        }
      }
      client.Close();

预期结果是浏览器将显示图像,就像您从imgur链接中看到的那样,基本上与我用来显示当前结果的屏幕快照的格式相同,是预期结果。 https://i.imgur.com/pxONnIP.png

1 个答案:

答案 0 :(得分:5)

HTTP指示初始行和标头应以path结尾( 不是 srcPath),并且双精度{ "pages": [ { "srcPath": "/content/dam/foo/about-bar/pdf/theplan.pdf", "srcTitle": "theplan.pdf", "path": "/content/foo/en/about-bar/the-plan-and-vision", "title": "the Plan and Vision", "references": [ "/content/foo/en/about-bar/the-plan-and-vision/jcr:content/content2/image/link", "/content/foo/en/about-bar/the-plan-and-vision/jcr:content/content2/textboximg/boxFtr", "/content/foo/en/about-bar/the-plan-and-vision/jcr:content/content1/textboximg/text" ], "published": false, "isPage": "true" } ] } { "pages": [] } { "pages": [] } { "pages": [ { "srcPath": "/content/dam/foo/about-bar/photos/rayDavis.PNG", "srcTitle": "rayDavis.PNG", "path": "/content/foo/en/about-bar", "title": "About bar", "references": [ "/content/foo/en/about-bar/jcr:content/content1B/promos_1/image/fileReference" ], "published": true, "isPage": "true" }, { "srcPath": "/content/dam/foo/about-bar/photos/rayDavis.PNG", "srcTitle": "rayDavis.PNG", "path": "/content/foo/en/about-bar/monkey-development/tales-of-giving/ray-moose-davis", "title": "ray moose Davis", "references": [ "/content/foo/en/about-bar/monkey-development/tales-of-giving/ray-moose-davis/jcr:content/content1/textboximg/fileReference" ], "published": true, "isPage": "true" }, { "srcPath": "/content/dam/foo/about-bar/photos/rayDavis.PNG", "srcTitle": "rayDavis.PNG", "path": "/content/foo/en/about-bar/monkey-development/tales-of-giving", "title": "tales of Giving", "references": [ "/content/foo/en/about-bar/monkey-development/tales-of-giving/jcr:content/content1/textboximg_2/fileReference" ], "published": true, "isPage": "true" } ] } { "pages": [ { "srcPath": "/content/dam/foo/about-bar/pdf/foo_19thNewsletter.pdf", "srcTitle": "foo_19thNewsletter.pdf", "path": "/content/foo/en/gremlins/stay-tuned", "title": "Stay tuned", "references": [ "/content/foo/en/gremlins/stay-tuned/jcr:content/content3/textboximg/text" ], "published": true, "isPage": "true" } ] } { "pages": [ { "srcPath": "/content/dam/foo/about-bar/pdf/barNews_fall1617.pdf", "srcTitle": "barNews_fall1617.pdf", "path": "/content/foo/en/gremlins", "title": "gremlins", "references": [ "/content/foo/en/gremlins/jcr:content/content2C/textboximg_114671747/text" ], "published": true, "isPage": "true" }, { "srcPath": "/content/dam/foo/about-bar/pdf/barNews_fall1617.pdf", "srcTitle": "barNews_fall1617.pdf", "path": "/content/foo/en/gremlins/stay-tuned", "title": "Stay tuned", "references": [ "/content/foo/en/gremlins/stay-tuned/jcr:content/content3/textboximg_0/text" ], "published": true, "isPage": "true" } ] } 应该将初始行和标题与响应主体分开。

这是HTTP协议的超实用快速指南:

https://www.jmarshall.com/easy/http/

在从对CRLF的操作切换为对其基本流进行操作之前,您可能还需要刷新LF

CRLF

我还避免使用StreamWriter,因为不能保证所有平台的行尾都相同。