java post xml 发送_java 发送post请求,内容为xml
一个web service 提供了post请求方式,用户发送一个xml请求文档比如:namezhangsaninfo服务端会返回zhangsan的信息。上面的xml文档在浏览器中通过post表单方式正常执行。但是在java中就不行了java种代码如下:package common.post;import java.io.BufferedReader;import java.io.FileInput
一个web service 提供了post请求方式,用户发送一个xml请求文档比如:
name
zhangsan
info
服务端会返回zhangsan的信息。上面的xml文档在浏览器中通过post表单方式正常执行。
但是在java中就不行了java种代码如下:
package common.post;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class PostTest {
void testPost(String urlStr) {
try {
URL url=new URL(urlStr);
URLConnection con=url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out=new OutputStreamWriter(con.getOutputStream());
String request=readFile();
System.out.println("Exedata satart\n"+request+"\nExe end");
out.write(new String(request.getBytes("ISO-8859-1")));
out.flush();
out.close();
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line="";
for(line=br.readLine();line!=null;line=br.readLine()) {
System.out.println(line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String readFile() {
StringBuilder sb=new StringBuilder();
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("E:/raw.xml")));
//这里的raw.file内容就是 上面那个xml片段,就是读取内容,将请求的xml保存成字符串 进行post发送
String line="";
for(line=br.readLine();line!=null;line=br.readLine()) {
sb.append(line+"\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
String url="http://localhost:9000/WebService";
new PostTest().testPost(url);
}
}
结果一直是
There went something wrong with parsing the POST data: Premature end of file.
这是什么原因
问题补充:
好像发现问题了:
我在webservice中有这个代码,是用来截取请求的字段的:
InputStream is=......;
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
//BufferedReader br=new BufferedReader(new InputStreamReader(is));
//FileOutputStream fos=new FileOutputStream("E:/is.xml");
//for(String line=br.readLine();line!=null;line=br.readLine()) {
//fos.write((line+"\n").getBytes());
//}
//fos.close();
//br.close();
// parse the InputStream to create a Document
doc = fac.newDocumentBuilder().parse(is);
后来我把中间那个网文件写入的代码注释后 就没问题了 是不是一个InputStream只能用一次啊?
如果我想实现如上功能,就是先写入到文件中,然后解析,这个InputStream该怎么搞?复制一个?
问题补充:
引用
你都已经先写到文件中了,再解析就应该是对文件的内容进行解析了,而不是再去得到原来的InputStream输入流再进行操作
写到文件只是为了测试用的,这么说InputStream写入到文件后,就不能继续用了么?
问题补充:
引用
不是,看错了你的代码,
你只用了一次,程序看来是没什么问题
现在问题已经解决了
1. InputStream is=......;
2. DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
3. fac.setNamespaceAware(true);
4. //BufferedReader br=new BufferedReader(new InputStreamReader(is));
5. //FileOutputStream fos=new FileOutputStream("E:/is.xml");
6. //for(String line=br.readLine();line!=null;line=br.readLine()) {
7. // fos.write((line+"\n").getBytes());
8. //}
9. //fos.close();
10. //br.close();
11. // parse the InputStream to create a Document
12. doc = fac.newDocumentBuilder().parse(is);
还是这个代码 如果注释部分去掉注释 也就是执行的话 就会出错 如果注释掉就没问题.
我现在的疑问是如果注释代码执行,InputStream就用了2次,是不是因为这个原因,第一次是写入文件,第二次是parse,第二次这个流是不是已经空了,所以异常发生?
引用
web service的调用,我怀疑是不能以out.write(new String(request.getBytes("ISO-8859-1")));把请求内容写进去就当是post
我觉得可能不是这个问题 因为con.setDoOutput(true); 就意味着post了
更多推荐
所有评论(0)