开发者社区 问答 正文

python 2.6 请求servlet报错?报错

我的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()



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:\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





展开
收起
爱吃鱼的程序员 2020-06-09 16:40:28 655 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    packagecom.demo;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassTestServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{System.out.println("执行get方阿飞");Stringstring=req.getParameter("account");System.err.println("接收到的数据为:"+string);resp.setContentType("text/xml;charSet=utf-8");resp.getWriter().write("OK200");resp.flush();}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{System.out.println("执行post方阿飞");Stringstring=req.getParameter("account");System.err.println("接收到的数据为:"+string);resp.getWriter().write("OK200");resp.flush();}}



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

    packagecom.demo;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassTestServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{System.out.println("执行get方阿飞");Stringstring=req.getParameter("account");System.err.println("接收到的数据为:"+string);resp.setContentType("text/xml;charSet=utf-8");resp.getWriter().write("OK200");resp.flush();}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{System.out.println("执行post方阿飞");Stringstring=req.getParameter("account");System.err.println("接收到的数据为:"+string);resp.getWriter().write("OK200");resp.flush();}}



    2020-06-09 16:40:45
    赞同 展开评论