在C#中响应HttpPost

时间:2014-08-07 17:03:16

标签: java c# http

我有两个应用程序,一个是Java,另一个是C#。使用Java我正在发送HttpPost并等待这样的响应:

try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(arg0[0]);
        httppost.setEntity(new StringEntity(arg0[1]));
        httpclient.execute(httppost);
        Log.v(TAG, "Sent: " + arg0[1] + " to " + arg0[0]);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        e.printStackTrace();
    }

当我使用此代码正确收到帖子时,会将帖子发送到C#:

socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

                info.Text = "Capturing on: " + ifaces.SelectedItem.ToString();
                capIface = ifaces.SelectedItem.ToString();
                socket.Bind(new IPEndPoint(ifs[ifaces.SelectedIndex], 0));

                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

                byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                byte[] byOut = new byte[4];

                socket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
                byte[] data = new byte[4096];
                socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

private void OnReceive(IAsyncResult ar)
    {
        try
        {
            int rec = socket.EndReceive(ar);

            ParseData(data, rec);

            if (capture)
            {
                data = new byte[4096];
                socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
            }
        }
        catch (ObjectDisposedException)
        {
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error receiving packets", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

但我不知道从C#应用程序发送HttpResponse以便我可以用Java读取它。我非常感谢你的帮助。

0 个答案:

没有答案