我的sendToJava.py中的程序如下:
#-*- coding:utf-8 -*-
ps_url = "http://10.143.239.253:8080/secondJavaDemo/TestServlet?account=guopengfei&receiver=0001"
filename="E:\PythonWork\Testdefdoc.xml"
file = open(filename, 'r')
content = file.read()
import urllib2
req = urllib2.Request(url=ps_url, headers = {'Content-type':'text/xml'})
req.add_data(content)
rep = urllib2.urlopen(req)
print rep.read()
package com.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("执行get方阿飞");
String string = req.getParameter("account");
System.err.println("接收到的数据为:"+string);
resp.setContentType("text/xml;charSet=utf-8");
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("执行post方阿飞");
String string = req.getParameter("account");
System.err.println("接收到的数据为:"+string);
super.doPost(req, resp);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>XmlRpcServer</servlet-name>
<servlet-class>com.demo.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XmlRpcServer</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
C:\Python27\python.exe C:/Users/huipu/Desktop/testpythonfile/sendToJava.py
Traceback (most recent call last):
File "C:/Users/huipu/Desktop/testpythonfile/sendToJava.py", line 15, in <module>
rep = urllib2.urlopen(req)
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 437, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed
进程已结束,退出代码1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。