Windows手机插座编程

时间:2011-08-24 13:47:12

标签: c# windows-phone-7.1

我想使用Windows Phone 7.5(MANGO)上的套接字连接到http服务器。代码如下所示:

public class sockets
    {
        // Buffer for incoming data
        private byte[] _receiveBuffer;

        public void Demo()
        {
            String host = "209.85.148.106";
            int port = 80;
        String Message = "GET / HTTP/1.1 Host: google.pl User-Agent: Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8 Accept-Language: pl,en-us;q=0.7,en;q=0.3 Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n";

        // create endpoint
        var ipAddress = IPAddress.Parse(host);
        var endpoint = new IPEndPoint(ipAddress, port);

        // convert text to send (prefix with length)
        //var message = string.Format("{0};{1}", Message.Length, Message);
        var buffer = Encoding.UTF8.GetBytes(Message);

        // create event args
        var args = new SocketAsyncEventArgs();
        args.RemoteEndPoint = endpoint;
        args.Completed += SocketAsyncEventArgs_Completed;
        args.SetBuffer(buffer, 0, buffer.Length);

        // create a new socket
        var socket = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream,
            ProtocolType.Tcp);

        // connect socket
        bool completesAsynchronously = socket.ConnectAsync(args);

        // check if the completed event will be raised.
        // if not, invoke the handler manually.
        if (!completesAsynchronously)
        {
            SocketAsyncEventArgs_Completed(args.ConnectSocket, args);
        }
    }

    private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
    {
        // check for errors
        if (e.SocketError != SocketError.Success)
        {
            System.Diagnostics.Debug.WriteLine(e.ToString());
            return;
        }

        // check what has been executed
        switch (e.LastOperation)
        {
            case SocketAsyncOperation.Connect:
                HandleConnect(e);
                break;
            case SocketAsyncOperation.Send:
                HandleSend(e);
                break;
            case SocketAsyncOperation.Receive:
                HandleReceive(e);
                break;
            case SocketAsyncOperation.ReceiveFrom:
                HandleReceive(e);
                break;
        }
    }

    private void HandleConnect(SocketAsyncEventArgs e)
    {
        if (e.ConnectSocket != null)
        {
            // simply start sending
            bool completesAsynchronously = e.ConnectSocket.SendAsync(e);

            // check if the completed event will be raised.
            // if not, invoke the handler manually.
            if (!completesAsynchronously)
            {
                SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
            }
        }
    }

    private void HandleSend(SocketAsyncEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("WYSLANO");

        // simply start sending
        bool completesAsynchronously = e.ConnectSocket.ReceiveAsync(e);

        // check if the completed event will be raised.
        // if not, invoke the handler manually.
        if (!completesAsynchronously)
        {
            SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
        }
    }

    private void HandleReceive(SocketAsyncEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("ODEBRANO");


    }
}

这部分代码来自手册等。它试图连接到http://google.pl是发送数据但是没有收到任何内容。

1 个答案:

答案 0 :(得分:1)

我发现您在邮件末尾发送了一个回车/换行符。您需要发送两封信号表示您完成了请求。

虽然更好的选择可能是使用WebClient类的HttpWebRequest类,因为它们都会为您发送正确构建的HTTP请求。