简体   繁体   English

是否可以通过 HTTP 或 FTP 协议而不是 TCP/IP 传输数据 stream

[英]Is it possible to transfer data stream via HTTP or FTP protocol instead of TCP/IP

The requirement is to transfer bytes data stream(in bytes format) through HTTP or FTP protocol.要求是通过 HTTP 或 FTP 协议传输字节数据流(以字节格式)。 I am doing it with IP protocol but as it is unable to be handled by load balancers.我正在使用 IP 协议执行此操作,但由于负载均衡器无法处理它。 Right now I am sending data stream by converting into byte format and at receiver's end decoding it into string form.现在我正在发送数据 stream 通过转换为字节格式并在接收端将其解码为字符串形式。 The same thing I wanna do with HTTP protocol.我想对 HTTP 协议做同样的事情。

Socket sock = new Socket(AddressFamily.InterNetwork, 
              SocketType.Stream, ProtocolType.IP);

Here in ProtocolType.IP there should be HTTP but HTTP option is not there.ProtocolType.IP中应该有 HTTP 但 HTTP 选项不存在。

This is a WinForms application.这是一个 WinForms 应用程序。

HTTP isn't a protocol at that level - it's an application level protocol. HTTP 不是该级别的协议 - 它是应用程序级别的协议。

You don't get "an HTTP socket" - you typically get a TCP/IP socket and write HTTP data over that.你不会得到“一个 HTTP 套接字”——你通常会得到一个 TCP/IP 套接字并在上面写入 HTTP 数据。

See the OSI model for more details about the network layers involved.有关所涉及的网络层的更多详细信息,请参阅OSI model

I don't know for sure whether the built-in HTTP client libraries in .NET support streaming requests... you'd probably want to turn buffering off and write to the request stream.我不确定 .NET 中的内置 HTTP 客户端库是否支持流式请求...您可能想要关闭缓冲并写入请求 stream。 See what that looks like at the socket level using something like WireShark .使用WireShark之类的东西查看套接字级别的外观。

The ProtocolType enumeration does not include HTTP. ProtocolType枚举不包括 HTTP。 HTTP is part of the application layer in the OSI model. HTTP 是 OSI model 中应用层的一部分。 There has never been a concept of a "HTTP Socket".从来没有“HTTP Socket”的概念。 HTTP is encapsulated within TCP/IP. HTTP 封装在 TCP/IP 中。 The ProtocolType enumeration refers to the transport protocol. ProtocolType枚举是指传输协议。

From MSDN on the ProtocolType enumeration ,MSDN 上的 ProtocolType 枚举

    IP  Internet Protocol.
    IPv6HopByHopOptions     IPv6 Hop by Hop Options header.
    Icmp    Internet Control Message Protocol.
    Igmp    Internet Group Management Protocol.
    Ggp     Gateway To Gateway Protocol.
    IPv4    Internet Protocol version 4.
    Tcp     Transmission Control Protocol.
    Pup     PARC Universal Packet Protocol.
    Udp     User Datagram Protocol.
    Idp     Internet Datagram Protocol.
    IPv6    Internet Protocol version 6 (IPv6).
    IPv6RoutingHeader   IPv6 Routing header.
    IPv6FragmentHeader  IPv6 Fragment header.
    IPSecEncapsulatingSecurityPayload   IPv6 Encapsulating Security Payload header.
    IPSecAuthenticationHeader   IPv6 Authentication header. For details, see RFC 2292 section 2.2.1, available at http://www.ietf.org.
    IcmpV6  Internet Control Message Protocol for IPv6.
    IPv6NoNextHeader    IPv6 No next header.
    IPv6DestinationOptions  IPv6 Destination Options header.
    ND  Net Disk Protocol (unofficial).
    Raw     Raw IP packet protocol.
    Unspecified     Unspecified protocol.
    Ipx     Internet Packet Exchange Protocol.
    Spx     Sequenced Packet Exchange protocol.
    SpxII   Sequenced Packet Exchange version 2 protocol.
    Unknown     Unknown protocol.

Look http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/7be22396-0f2a-4138-b47a-09d93894185b The first post shows how to send some bytes through a HTTP-Request.http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/7be22396-0f2a-4138-b47a-09d93894185b第一篇文章展示了如何通过 HTTP 请求发送一些字节。 Adapted for your use:适合您的使用:

byte[] data;
// ...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(server);
request.ContentType = "application/octet-stream";
request.Method = "POST"; // I think, your data is too big for GET
request.ContentLength = data.Length;
try {
    Stream postStream = request.GetRequestStream();
    postStream.Write(data, 0, data.Length);
    postStream.Close();
} catch(Exception ex) {
    throw new Exception(ex.Message);
}

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

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