如何将URL参数发送到套接字连接

本文关键字:套接字 连接 参数 URL | 更新日期: 2023-09-27 18:25:32

我目前正在处理一个需要使用TCP或Socket连接的项目。因为我得到了一个URL,让我连接到它,服务器会把数据推送给我

给定的URL是http://member.sugacane.com/userInfo?userid=2&token=SomeToken

我浏览MSDN(http://msdn.microsoft.com/en-us/library/kb5kfec7.aspx)并获取一些样本代码进行处理。

public static void StartClient(string Host, string token, int userID, int port)
        {
            Host = "member.sugacane.com";
            string Datamsg = "userid=2&token=SomeToken";
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            try
            {
                //  uses port 80.
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostEntry(Host).HostName); 
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 
                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                // Connect the socket to the remote endpoint. Catch any errors.
                try
                {
                    sender.Connect(remoteEP);
                    Console.WriteLine("Socket connected to {0}",
                        sender.RemoteEndPoint.ToString());
                    string isConnect = "Socket connected to {0}" +sender.RemoteEndPoint.ToString();
                    // Encode the data string into a byte array.
                    byte[] msg = Encoding.ASCII.GetBytes(Datamsg);
                    // Send the data through the socket.
                    int bytesSent = sender.Send(msg);
                    // Receive the response from the remote device.
                    int bytesRec = sender.Receive(bytes);
                    string res = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    // Release the socket.
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

我可以用这段代码连接到服务器,但我无法得到任何响应,字节总是0,它应该返回一些XML数据。我想知道我发送了错误的Datamsg

如何将URL参数发送到套接字连接

您应该使用http协议。查看维基百科文章中的示例会话:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol.仅发送url是不足以让服务器做出响应的。

它看起来像这样:

请求:

GET /index.html HTTP/1.1
Host: www.example.com

回应:

HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Connection: close
<html>
<head>
  <title>An Example Page</title>
</head>
<body>
  Hello World, this is a very simple HTML document.
</body>
</html>

您可以使用telnet来检查您尝试发送的消息是否正确。另一种选择是在浏览器中粘贴链接,并使用Fiddler或Wireshark检查发送到服务器的内容。

更新

例如,如果你像这样更改代码的第一行,你应该从维基百科得到响应:

Host = "en.wikipedia.org";
string Datamsg = @"GET /wiki/Main_Page HTTP/1.1
Host: en.wikipedia.org
";

所以你的代码应该看起来像(我留下了你应该放置有效令牌的部分,等等):

Host = "member.sugacane.com";
string Datamsg = @"GET /userInfo?userid=2&token=SomeToken HTTP/1.1
Host: member.sugacane.com
";

我想知道是我发送了错误的数据消息

如果您连接的服务器是HTTP服务器,那么是的,您发送的请求不正确。为此,请使用类似HttpClientHttpWebRequest的库。