-2

我正在编写一个应用程序,它将通过 HTTP 将 XML 发送到服务器,并接收 XML 作为响应。我能够将 XML 发送到服务器,但无法接收响应。

这是我的客户代码:

public void sendXMLToServer(){
    System.out.println("sendXMLToServer");
    String strURL = "http://localhost:9080/MockServerMachine/sendXMLPost";
    // Get file to be posted
    String strXMLFilename = "output.xml";
    File input = new File(strXMLFilename);
    // Prepare HTTP post
    System.out.println("junaud url "+ strURL);
    PostMethod post = new PostMethod(strURL);


 // Request content will be retrieved directly
    // from the input stream
    // Per default, the request content needs to be buffered
    // in order to determine its length.
    // Request body buffering can be avoided when
    // content length is explicitly specified
    try {
        post.setRequestHeader("Content-type","application/xml");
        post.setRequestHeader("Accept","application/xml");

        post.setRequestEntity(new InputStreamRequestEntity(
                new FileInputStream(input), input.length()));
        HttpClient httpclient = new HttpClient();
        int result = httpclient.executeMethod(post);
        String xmlResponse = post.getResponseBodyAsString();
        // Display status code
        System.out.println("Response status code jun: " + result);

        // Display response
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
        post.releaseConnection();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是服务器端:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //InputStream in = request.getInputStream();
        //URL xmlUrl = new URL(request.getRequestURL().toString());
    //InputStream in = xmlUrl.openStream();

    response.setContentLength(100);
//      PostMethod po = new PostMethod(request.getRequestURL().toString());
//      System.out.println("kikmk = "+po.getRequestEntity());

    try {
        // read this file into InputStream
        //InputStream inputStream = new FileInputStream("c:\\file.xml");
        InputStream inputStream = request.getInputStream();
        // write the inputStream to a FileOutputStream
        OutputStream out = new FileOutputStream(new File("c:\\junaidAhmedJameel.xml"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            System.out.println(new String (bytes));
            System.out.println(read);
            out.write(bytes, 0, read);
        }

        inputStream.close();
        out.flush();
        out.close();

        System.out.println("New file created!");
        } catch (IOException e) {
        System.out.println(e.getMessage());
        }

      }

有谁可以帮我离开这里吗?任何通过 HTTP 发送 XML 的示例客户端/服务器示例都很棒。

4

2 回答 2

2

啊,发现了。看这里:

OutputStream out = new FileOutputStream(new File("c:\\junaidAhmedJameel.xml"));

那只是要写入本地磁盘。您没有将任何内容写入响应流。目前尚不清楚您要向响应流写入什么,但明显没有调用response.getWriter()or response.getOutputStream()

您将内容长度设置为 100,但实际上并未发送任何内容。请注意,无论如何硬编码内容长度几乎肯定是错误的事情......但是当您不发送任何内容时,这绝对是错误的事情......

于 2013-01-01T09:43:22.810 回答
0

您永远不会在服务器代码中生成任何响应内容。您只需将长度设置为 100。

于 2013-01-01T09:43:14.523 回答