如何用http post的方式发送一个xml数据,并且response一个xml数据!!!急等,谢谢

jiab 2008-03-12 05:35:56
我有一个xml文档,如下:
<?xml version="1.0" encoding="UTF-8"?>
<msg>
<title>标题</title>
<content>内容</content>
<in_ip>内网ip</in_ip>
<out_ip>外网ip</out_ip>
<level>告警级别</level>
<type>监控类型</type>
<sub_time>消息提交时间</sub_time>
<happen_time>故障发生时间</happen_time>
</msg>
我想通过post方式到地址http://<host>:<port>/monitor/ReceiveMsg,然后返回一个xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
<hRet>200</hRet>
</response>

请问用java怎么写呢?谢谢
...全文
1991 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
月夜之影 2008-09-08
  • 打赏
  • 举报
回复
现在正好碰见一个和搂主一样的问题,麻烦搂主帮忙解决一下
chjx1982 2008-03-18
  • 打赏
  • 举报
回复
不懂,顶
lifaming15 2008-03-18
  • 打赏
  • 举报
回复
看楼主是不是一个域咯
ajax是不能直接跨域访问,但是有第3方包支持跨域访问
ajax是好是坏,就看怎么用了
lifaming15 2008-03-14
  • 打赏
  • 举报
回复
用了servlet就可以不用什么HttpURLConnection了.
使用ajax方式请求,发送XML数据;在servlet里,解析xml,再响应xml
jiab 2008-03-14
  • 打赏
  • 举报
回复
我处理xml成功或是失败的代码写在哪里,比如处理成功还要返回一个xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
<hRet>200</hRet>
</response>
是要还得写一个servlet吗???
老紫竹 2008-03-14
  • 打赏
  • 举报
回复
楼上的看清楚,这个是服务器端的行为,
Ajax是不能跨域直接调用的。
而且Ajax会把业务细节暴露给用户,这是有风险的。
zhuyx808 2008-03-13
  • 打赏
  • 举报
回复
MARK 紫竹回答的帖子
jiab 2008-03-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 java2000_net 的回复:]
用 HttpURLConnection 也可以,请参考
Java codeURL url=newURL(".......");
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setDoOutput(true);//POST方式con.setRequestMethod("POST");
OutputStream os=con.getOutputStream();//输出流,写数据os.write("..........".getBytes());
BufferedReader reader=newBufferedReader(newInputStreamReader(con.getInputStream())…
[/Quote]

用着这方法我是不是应该写个servlet,通过request获得xml,然后解析校验,如果成功则response 200
老紫竹 2008-03-13
  • 打赏
  • 举报
回复
用 HttpURLConnection 也可以,请参考
      URL url = new URL("......."); 
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true); // POST方式
con.setRequestMethod("POST");
OutputStream os = con.getOutputStream(); // 输出流,写数据
os.write("..........".getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); // 读取结果
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}


FROM:http://www.java2000.net/viewthread.jsp?tid=302
老紫竹 2008-03-13
  • 打赏
  • 举报
回复
用 HttpURLConnection 也可以,请参考
      URL url = new URL("......."); 
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true); // POST方式
con.setRequestMethod("POST");
OutputStream os = con.getOutputStream(); // 输出流,写数据
os.write("..........".getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); // 读取结果
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}


FROM:http://www.java2000.net/viewthread.jsp?tid=302
ganguozai 2008-03-12
  • 打赏
  • 举报
回复
基本思路:在服务端构建dom树,然后解析发送的xml字符串,最后拼装xml字符串响应请求。
具体实现:可参考《AJAX基础教程》中用xml发送数据的例子。
favorite7w 2008-03-12
  • 打赏
  • 举报
回复
用apache的项目:httpClient可以模拟post发送文本数据流。
以下代码并不完整,需要你查资料修改补齐。

// 设置连接的基本参数
HttpClient client = new HttpClient();
List authPrefs = new ArrayList(1);
authPrefs.add(AuthPolicy.DIGEST);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client.getState().setCredentials(new AuthScope(null, -1, null),
new UsernamePasswordCredentials("username", "password"));
// 连接
PostMethod method = new PostMethod(connectionURL);

// 发送XML报文
method.setRequestEntity(new StringRequestEntity("要传送的XML内容", "text/html", "utf-8"));
client.executeMethod(method);

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧