JAVA以POST方式提交XML获取返回值(返回格式为XML)
我想写一个方法:Stringurl="http://123.12.12.12:8909/asdasd";Stringxml="<?xmlversion="1.0"enco...
我想写一个方法:
String url = "http://123.12.12.12:8909/asdasd";
String xml = "<?xml version="1.0" encoding="utf-8"?><request><hRet>0</hRet><transIDO>12345678901234567</transIDO></request>"
public String toSendXML(String url, String xml){
/*
这里该怎么写 哪位大神帮我下 谢谢了, 我用了网上的好多方法 但是获取不到返回值
*/
}
希望能弄得详细点 在线等 急啊 展开
String url = "http://123.12.12.12:8909/asdasd";
String xml = "<?xml version="1.0" encoding="utf-8"?><request><hRet>0</hRet><transIDO>12345678901234567</transIDO></request>"
public String toSendXML(String url, String xml){
/*
这里该怎么写 哪位大神帮我下 谢谢了, 我用了网上的好多方法 但是获取不到返回值
*/
}
希望能弄得详细点 在线等 急啊 展开
1个回答
展开全部
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setRequestMethod(“POST”);
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(xml);
outputStream.close();
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setRequestMethod(“POST”);
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(xml);
outputStream.close();
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
追问
请问能否再详细点呢 比如buffer.append(str); 的buffer是哪里来的
追答
StringBuffer buffer = new StringBuffer();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询