python 2.6 请求servlet报错

发布于 2021-11-28 21:04:19 字数 3471 浏览 891 评论 2

我的sendToJava.py中的程序如下:

#-*- coding:utf-8 -*-
ps_url = "http://10.143.239.253:8080/secondJavaDemo/TestServlet?account=guopengfei&receiver=0001"
filename="E:PythonWorkTestdefdoc.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()



java中的servlet程序如下:
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);
	}

}



web.xmlru如下:
<?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>



运行python报错信息如下:
C:Python27python.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:Python27liburllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:Python27liburllib2.py", line 437, in open
    response = meth(req, response)
  File "C:Python27liburllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:Python27liburllib2.py", line 475, in error
    return self._call_chain(*args)
  File "C:Python27liburllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:Python27liburllib2.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





如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2021-11-30 00:10:38

引用来自“蓝水晶飞机”的评论

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");

        resp.getWriter().write("OK 200");
        resp.flush();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("执行post方阿飞");
        String  string = req.getParameter("account");
        System.err.println("接收到的数据为:"+string);

        resp.getWriter().write("OK 200");
        resp.flush();
    }
 
}

千笙结 2021-11-29 22:36:54
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");

        resp.getWriter().write("OK 200");
        resp.flush();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("执行post方阿飞");
        String  string = req.getParameter("account");
        System.err.println("接收到的数据为:"+string);

        resp.getWriter().write("OK 200");
        resp.flush();
    }
 
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文