1

我正在使用Android 1.6。在我的办公室里,有一些应用程序通过 HTTP post 方法发送一些数据。

我想添加一个模块通过套接字发送数据。我通过套接字(输出流)正确发送数据。它在服务器端(c#套接字应用程序)套接字应用程序中正确接收。

但是在 HTTP post 方法中,一些数据作为参数传递。我无法在套接字中找到任何方法来发送一些数据作为参数和流中的一些数据。

以下是我通过 outpustream 通过套接字发送数据的代码。

               socket=new Socket(this.ipAddress,this.port_number);

            //socket.setSocketImplFactory(fac)
            Log.i(tagName, "after creating sokcet");

            os=socket.getOutputStream();
            is=socket.getInputStream();

            dos=new DataOutputStream(os);               
            Log.i(tagName, "after creating ouput streams");

            dis=new DataInputStream(is);
            Log.i(tagName, "after creating input streams");

            //dos.writeUTF(msg[i].trim());
            //dos.write(msg[i].trim().getBytes());

            //dos.writeUTF(msg[i].trim());
            dos.write(msg[i].trim().getBytes()); //data written as bytes
            //dos.writeUTF(str)
            dos.flush();

            Log.i(tagName, "after writing data to os");

            StringBuilder sbuilder=new StringBuilder();

            ///*
            int ch;
            byte bt=1;
            while((bt=(byte) dis.read())!=-1)
            {
                Log.i(tagName, "ch="+bt);
                byte temp[]=new byte[1];
                //temp[0]=(byte)ch;
                temp[0]=(byte)bt;
                String tempStr1=new String(temp);
                Log.i(tagName, "tempstr:"+tempStr1);

                sbuilder.append(tempStr1);

                Log.i(tagName, "Data fro server : "+sbuilder.toString());
                tempStr1=null;
            }
            //*/
            //byte tt[]=new byte[dis.readLine()]
            //resultStr=dis.readLine();resultStr=resultStr.trim();
            resultStr=sbuilder.toString();
            Log.i(tagName, "server res :"+resultStr);



            if(dos!=null)
            {
                try
                {
                    dos.close();
                }
                catch(Exception ex)
                {

                }
            }

            if(dis!=null)
            {
                try
                {
                    dis.close();
                }
                catch(Exception ex){}
            }
            if(socket!=null)
            {
                try
                {
                    socket.close();
                }
                catch(Exception ex)
                {

                }
            }

上面的代码片段可以正常工作。

但在 HTTP post 方法中,用户名和密码作为参数和流中的实际数据。就像那样,我想在参数中发送用户名和密码,并在流中发送实际的用户名和密码。

4

1 回答 1

0


我找到了解决方案。
我们不能将数据作为参数发送,如套接字连接中的 http 参数。
我们只能在流中发送数据。
在服务器端,我们可以编写来解析流中的数据。

谢谢

于 2012-07-16T10:04:28.390 回答