簡體   English   中英

如何在Restful Web服務中發送XML文件端點URL

[英]How to sent XML file endpoint url in Restful web service

我需要將請求XML文件作為多部分表單數據發送到{url}。 這在Restful Web服務中是如何進行的。 在我在那里使用之前,

RequestDispatcher rd = request.getRequestDispatcher("/file/message.jsp");
rd.forward(request, response);

但這不是在特定的{url}中發送的,如何發送?

您可以使用Jersey Rest Client作為post request發送XML消息。

try {
    Client client = Client.create();

    WebResource webResource = client.resource(http://<your URI>);

    // POST method
    ClientResponse response = webResource.accept("multipart/form-data").type("multipart/form-data").post(ClientResponse.class, "<your XML message>");

    // check response status code
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    // display response
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... ");
    System.out.println(output + "\n");
} catch (Exception e) {
     e.printStackTrace();
}

對於Jersey客戶,您可以在這里找到文檔:

澤西島REST客戶端

網絡資源

如果可以做到(取決於您的上下文),則使用JAX-RS客戶端是一種解決方案。

Apache CXF的示例:

InputStream inputStream = getClass().getResourceAsStream("/file/message.jsp");
WebClient client = WebClient.create("http://myURL");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=message.jsp");
Attachment att = new Attachment("root", inputStream, cd);
client.post(new MultipartBody(att));

我認為網絡服務不是您的最佳選擇
您可以嘗試本機流,然后可以根據需要編寫標題和正文,這里是示例代碼來解釋我的觀點

Socket socket=new new socket(InetAddress.getByName("stackoverflow.com"), 80);
// just the host and the port
Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
            out.write("POST http://" + HOST + ":" + port+ "/ HTTP/1.1\r\n");//here u can insert your end point or the page accepts the xml msg
    out.write("Host: " + HOST + "/ \r\n");
    out.write("Content-type: application/xml,text/xml\r\n");// Accept
    out.write("Content-length: " + req.length() + "\r\n");
    out.write("Accept:application/xml,text/xml\r\n");
    out.write("\r\n");
    // req the Request Body or the xml file to be sent
    out.write(req);//
    out.flush();`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM