0

我已经通过 wifi 在我的 Win-7 笔记本电脑和三星智能手机 (Bada OS 2.0) 之间创建了一个 ad-hoc 网络连接。我还通过验证 ip 地址并从笔记本电脑 ping 我的智能手机来检查连接性。

现在,我想编写一个代理服务器,它位于我的 Win-7 上,在端口 8080 上接受来自智能手机的 HTTP 请求,从互联网检索网页,并将其传递回智能手机。这个想法是双重的:在我的智能手机上访问互联网,并学习关于移动设备的网络编程。我编写了以下 c-sharp 代码,它实现了 TCPListener 以接收请求并发送回响应,这非常好:

        private void listen()
    {
        try
        {
            listener.Start();
            while (true)
            {
                Socket client= listener.AcceptSocket();
                ProcessClient(client);
            }
        }
        catch (ThreadAbortException ex)
        {
            WriteLog("Error occured: " + ex.Message);
        }
    }

    private void ProcessClient(Socket client)
    {
        //extract client data
        byte[] bytes=new byte[1024];
        int size = client.Receive(bytes, bytes.Length, SocketFlags.None);

        string clientmessage = System.Text.Encoding.ASCII.GetString(bytes);

        if (size == 0)
            return;

        int index1  = clientmessage.IndexOf(@" ");
        int index2 = clientmessage.IndexOf(@" ", index1 + 1);
        if (index1 == -1 || index2 == -1)
        {
            throw( new Exception());
        }

        //extract client url
        string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
        int index3 = part1.IndexOf(@"/", index1 + 8);
        int index4 = part1.IndexOf(@" ", index1 + 8);
        int index5 = index4 - index3;
        string sURL= part1.Substring(index1 + 4, part1.Length - index5 - 8);

        //send request to web server
        IPHostEntry serverHost = Dns.GetHostEntry(sURL);
        string[] aliases = serverHost.Aliases;
        IPAddress[] addresses = serverHost.AddressList;
        IPEndPoint serverEP = new IPEndPoint(addresses[0], 80);
        Socket server = new Socket((addresses[0].AddressFamily), SocketType.Stream, ProtocolType.Tcp);
        server.Connect(serverEP);

        //server.Send(bytes, bytes.Length, SocketFlags.None);
        byte[] sendbytes = new byte[1024];
        size = System.Text.Encoding.ASCII.GetBytes(clientmessage, 0, clientmessage.Length, sendbytes, 0);
        server.Send(sendbytes, sendbytes.Length, SocketFlags.None);
        //server.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(clientmessage), System.Text.ASCIIEncoding.ASCII.GetByteCount(clientmessage), SocketFlags.None);

        //read response from web server
        byte[] rbytes = new byte[4096];
        string strPage = "";
        size = 1;
        while (size > 0)
        {
            size = server.Receive(rbytes, rbytes.Length, SocketFlags.None);
                strPage += System.Text.Encoding.ASCII.GetString(rbytes,0,size);
        }
        server.Shutdown(SocketShutdown.Both);
        server.Close();

        //send the page back to the client
        //bytes = System.Text.Encoding.ASCII.GetBytes(strPage);
        bytes = new byte[strPage.Length+1];
        size = System.Text.Encoding.ASCII.GetBytes(strPage,0,strPage.Length,bytes,0);
        client.Send(bytes, size, SocketFlags.None);
    }

上面的代码从客户端(智能手机浏览器)获取请求,获取请求头部分并将其传递给网络服务器,然后通过相同的客户端套接字将响应中继回智能手机。这一切正常。出了问题的是网络服务器正在发送“错误请求 - 状态代码 400”的响应。我很惊讶这种情况正在发生,因为我所做的只是在两个端点之间来回转发请求。你能分享一下这个吗?以下是我通过分别获取变量 clientmessage 和 strPage 获得的发送的请求标头和接收的响应标头:

要求:

GET http://prahlad-tosh/welcome.png HTTP/1.1
Proxy-Connection: Keep-Alive
Host: prahlad-tosh
Accept: text/html, application/xml, image/vnd.wap.wbmp, image/png, image/jpeg, image/gif, image/bmp, application/vnd.wap.xhtml+xml, application/xhtml+xml, application/vnd.wap.multipart.mixed, multipart/mixed, application/vnd.oma.dd+xml, text/vnd.sun.j2me.app-descriptor, application/java-archive, application/vnd.youtube, */*
Accept-Language: en
X-Wap-Proxy-Cookie: none
Accept-Charset: utf-8;q=1.0,iso-8859-1;q=0.6
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S5380K/S5380KDDKK6; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile HVGA SMM-MMS/1.2.0 OPN-B
Referer: `http://prahlad-tosh/`

回复:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 19 Jun 2012 14:10:14 GMT
Connection: close
Content-Length: 326
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/htm/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>
4

1 回答 1

1

编辑:我认为问题可能出在服务器上。改用 Apache 或连接到 google.com 怎么样?

旁注:我认为您不需要http://prahlad-toshGET 请求中的部分,就/welcome.png可以了。

于 2012-06-19T15:04:30.630 回答