GET在C#中的TCP连接中是”GET”。

huangapple go评论71阅读模式
英文:

GET in TCP connection with C#

问题

I have translated the provided Delphi code into C#. Here is the translated code:

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main()
    {
        TcpClient client = new TcpClient();
        string host = "192.168.52.203";
        int port = 80;
        
        try
        {
            client.Connect(host, port);
            if (client.Connected)
            {
                NetworkStream stream = client.GetStream();
                StreamWriter writer = new StreamWriter(stream);
                StreamReader reader = new StreamReader(stream);

                string request = "GET /state.xml?noReply=0 HTTP/1.1\r\nAuthorization: Basic bm9uZTp3ZWJyZWxheQ==\r\n\r\n";
                writer.Write(request);
                writer.Flush();

                StringBuilder response = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                do
                {
                    bytesRead = reader.Read(buffer, 0, buffer.Length);
                    response.Append(buffer, 0, bytesRead);
                }
                while (bytesRead > 0);

                Console.WriteLine(response.ToString());
            }
            else
            {
                Console.WriteLine("Failed to connect to the server.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            client.Close();
        }
    }
}

This C# code replicates the functionality of the Delphi code, making a request to the specified host and port and printing the response.

英文:

I have written a program in Delphi with make a request to TCP. As I am beginning to work with C#, I try to translate it. Follow the code in Pascal, but for instance I don't know how to make it work in C#:


var
  s:shortstring;
  CR,LF:char;
  l:integer;
  buffer:TIdBytes;
  resp: TStringStream;
  ouvert:integer;
begin
  TCP:=TIdTCPClient.Create;
  CR:=chr(13);
  LF:=chr(10);
  TCP.Host:='192.168.52.203';
  TCP.Port:=80;
  TCP.Connect;
  if TCP.Connected then
  begin
    s:='GET /state.xml?noReply=0 HTTP/1.1'+CR+LF+'Authorization: Basic bm9uZTp3ZWJyZWxheQ=='+CR+LF+CR+LF;
    l:=Length(s);
    SetLength(buffer,l);
    Move(s[1],Buffer[0],l);
    TCP.IOHandler.Write(Buffer,l);
    resp := TStringStream.Create;
    TCP.IOHandler.ReadStream(resp,185);
    resp.Position:=0;
//    memoGet.Lines.LoadFromStream(resp);
    resp.Position:=0;
    s:=Resp.ReadString(185);

Thanks for your help.
Michel

答案1

得分: 0

以下是您提供的代码的中文翻译:

您可以通过TcpClient和System.net.sockets命名空间中的NetworkStream将其翻译为C#。

代码示例如下:

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main()
    {
        var tcp = new TcpClient();
        var cr = "\r";
        var lf = "\n";
        var host = "192.168.52.203";
        var port = 80;
        tcp.Connect(host, port);
        if (tcp.Connected)
        {
            var request = "GET /state.xml?noReply=0 HTTP/1.1" + cr + lf + "Authorization: Basic bm9uZTp3ZWJyZWxheQ==" + cr + lf + cr + lf;
            byte[] buffer = Encoding.ASCII.GetBytes(request);
            var stream = tcp.GetStream();
            stream.Write(buffer, 0, buffer.Length);
            var reader = new StreamReader(stream);
            var response = reader.ReadToEnd();
            Console.WriteLine(response);
        }
    }
}

希望这有助于您的项目!

英文:

You can translate it to C# via the TcpClient and NetworkStream usings from System.net.sockets namespace.

It may look like this:

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main()
    {
        var tcp = new TcpClient();
        var cr = "\r";
        var lf = "\n";
        var host = "192.168.52.203";
        var port = 80;
        tcp.Connect(host, port);
        if (tcp.Connected)
        {
            var request = "GET /state.xml?noReply=0 HTTP/1.1" + cr + lf + "Authorization: Basic bm9uZTp3ZWJyZWxheQ==" + cr + lf + cr + lf;
            byte[] buffer = Encoding.ASCII.GetBytes(request);
            var stream = tcp.GetStream();
            stream.Write(buffer, 0, buffer.Length);
            var reader = new StreamReader(stream);
            var response = reader.ReadToEnd();
            Console.WriteLine(response);
        }
    }
}

答案2

得分: 0

Thanks a lot, it's working fine. I have now a small program written in C# able to tell me if the roof of my automatic astronomical observatory is open.
Michel

英文:

Thanks a lot, it's working fine. I have now a small program written in C# able to tell me if the roof of my automatic astronomical observatory is open.
Michel

huangapple
  • 本文由 发表于 2023年4月19日 18:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定