指定したURL先のサーバにxmlを送信したいです。
どこに問題があるか、ご教授いただけるでしょうか。
#該当のソースコード
呼び出し側
JAVA
1status = service.distribute(xml,URL);
HTTP送信ソース
JAVA
1/* 2xml = <id>100</id> 3 <info>1008</info> 4url = http://hostname:8085/order/test 5*/ 6public HttpStatus distribute(String xml,String url) throws Exception { 7 8 try { 9 URL newurl = new URL(url); 10 HttpURLConnection connection = (HttpURLConnection) newurl.openConnection(); 11 connection.setRequestMethod("POST"); 12 connection.setDoOutput(true); 13 connection.setRequestProperty("Content-Type","application/yang-data+xml"); 14 connection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes())); 15 16// xml = xml.replaceAll("\r\n|\r|\n", ""); 17 18 System.out.println("url :"+url); 19 System.out.println("xml :"+xml); 20 21 PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8"))); 22 pw.print(xml);// content 23 24 // body部の文字コード取得 25 String contentType = connection.getHeaderField("Content-Type"); 26 String charSet = "Shift-JIS";//"ISO-8859-1"; 27 for(String elm : contentType.replace(" ", "").split(";")) { 28 if (elm.startsWith("charset=")) { 29 charSet = elm.substring(8); 30 break; 31 } 32 } 33 // body部受信 34 BufferedReader br; 35 try{ 36 br = new BufferedReader(new InputStreamReader( 37 connection.getInputStream(),charSet)); 38 } 39 catch(Exception e_){ 40 System.out.println( connection.getResponseCode() 41 +" "+ connection.getResponseMessage() ); 42 br = new BufferedReader(new InputStreamReader( 43 connection.getErrorStream(),charSet)); 44 } 45 String line; 46 while ((line = br.readLine()) != null) { 47 System.out.println(line); 48 } 49 br.close(); 50 connection.disconnect(); 51 } 52 catch(Exception e){ 53 e.printStackTrace(System.err); 54 } 55 56 return status; 57 } 58}
上記のプログラムで実施したいのは、以下のcurlコマンドと同じことです。
curl -v -X POST -T (xmlファイルの絶対パス) -u admin:admin http://hostname:8085/order/test
#実行結果/エラー内容
<errors xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf"> <error> <error-message>End-of-file reached in XML stream</error-message> <error-path>/tailf-ncs:services/sdn-n-svm:order</error-path> <error-tag>malformed-message</error-tag> <error-type>application</error-type> </error> </errors>400 Bad Request
#環境
java8
tomcat9
Springbootフレームワーク

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。