我正在尝试在URL调用中传递XML,但似乎无法正确获取参数。我被退回415 - 媒体类型无效。我知道如果我在我的REST客户端(在Firefox中)使用相同的XML运行此URL,一旦我放入application / xml标头,它就可以正常工作。但是,当我尝试在我的Java程序中执行相同操作时,我得到415错误。
我哪里错了?我想我正在定义内容类型。我在获取输入流的行上得到错误。
*****************我的代码**************************** *******
var myApp = angular.module('myApp', []);
myApp.service("initState", function(){
return{
initCount:0,
finishStep:function(){
this.initCount++;
}
}
});
myApp.directive("wizard",function(initState){
return {
link: function($scope){
$scope.$watch(function(){
return initState.initCount;
}, function(){
console.log('init count', initState.initCount );
if( initState.initCount === 2){
console.log('link on wizard');
}
})
},
restrict: "E",
template: "<step-1></step-1><step-2></step-2>"
}
});
myApp.directive("step1", function(initState){
return {
link: function(){
console.log('link on step1');
initState.finishStep();
},
restrict: "E",
template: "<h1>Step1</h1>"
}
});
myApp.directive("step2", function(initState){
return {
link: function(){
console.log('link on step2');
initState.finishStep();
},
restrict: "E",
template: "<h1>Step2</h1>"
}
});
*****************我的代码**************************** *******
*****************我的XML **************************** ******
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/xml; charset=\"utf-8\"");
connection.setRequestProperty("Authorization", "Basic " + new String(new Base64().encode((strUserName + ":" + strPassword).getBytes())));
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream(), "UTF8");
output.write(strXML);
output.flush();
Document document = builder.parse(connection.getInputStream());
//pull out nodes building a node list
nodeList = document.getElementsByTagName(strElement);
connection.disconnect();
*****************我的XML **************************** ******
答案 0 :(得分:0)
我发现了我的问题。似乎我插入我的字符串变量的XML缺少一些关键空格。具体来说,我将XML放在属性文件中(以简化更改)。当我这样做时,我不经意地把延续字符放在行尾的一些空格中。所以,为了解决我的问题,我做了两件事:
1)在几个地方增加了所需的空间 2)删除&#34; charset = utf-8&#34;来自content-type属性,以便我的Content-Type的setRequestProperty现在只设置为&#34; application / xml&#34;
感谢JP Moresmau成为我的声音板。它帮助我思考解决方案,最终找到了我的问题。