1

JavaScript では、次の実装があります。

function sendRMSRequest(xmlString, url){

    if(window.XMLHttpRequest){
        xmlRequest = new XMLHttpRequest();
    }

    xmlRequest.open("POST", url, false);
    xmlRequest.onreadystatechange = function() {processRequest();};
    xmlRequest.send(xmlString);
}

xmlString には次の文字列があります。

<TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'></TestRequest>

この機能をサーバー側に移動したいのですが、Java でこの機能を実装する方法がわかりません。

現在、次のJavaコードがあります。

public static void main(String[] args) throws IOException {
    String endPoint = "http://????.????/service";


    String xml =  "<TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'></TestRequest>";

    System.out.println(xml);

    URL url = new URL(endPoint);
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setDoInput(true);
    httpCon.setDoOutput(true);
    Authenticator.setDefault(new MyAuthenticator());

    httpCon.setReadTimeout(0);
    httpCon.setConnectTimeout(0);
    httpCon.setRequestMethod("POST");
    httpCon.setRequestProperty("Content-Type", "text/plain"); 
    httpCon.setRequestProperty("charset", "utf-8");
    httpCon.setRequestProperty("", xml);


    PrintWriter pw = new PrintWriter(httpCon.getOutputStream());  

    pw.write(xml);
    pw.flush();
    pw.close();  

    System.out.println(httpCon.getResponseCode());
    System.out.println(httpCon.getResponseMessage());
    System.out.println(httpCon.getContentType());



    String returnString = null;
    StringBuffer sb = null;
    BufferedInputStream in;

    // set up httpConn code not included same as previous
    in = new BufferedInputStream(httpCon.getInputStream());  

    int x = 0;

    sb = new StringBuffer();

    while ((x = in.read()) != -1) {
        sb.append((char) x);
    }

    in.close();
    in = null;

    if (httpCon != null) {
        httpCon.disconnect();
    }

    returnString = sb.toString();   
    System.out.println("Return String = " + returnString);

}

上記のコードを実行すると、次のエラーが発生します。

org.xml.sax.SAXParseException: Premature end of file.

どんな助けでも大歓迎です。

ありがとうダニエル

4

0 に答える 0