使用urllib发送XML

0 投票
1 回答
864 浏览
提问于 2025-04-16 21:05

根据这个 链接,我尝试用GET方法把一个XML文件发送到我的网络服务:

import urllib
from createfile import XML


URL = "http://http://localhost:8080/mywebservice

parameter = urllib.urlencode({'XML': XML})

response = urllib.urlopen(URL + "?%s" % parameter)

print response.read()

但是我遇到了这个错误:

Traceback (most recent call last):
  File "C:\eclipse\testing_workspace\http tester\src\Main.py", line 15, in <module>
    response = urllib.urlopen(URL + "?%s" % parameter)
  File "C:\Python27\lib\urllib.py", line 84, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 205, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 331, in open_http
    h = httplib.HTTP(host)
  File "C:\Python27\lib\httplib.py", line 1047, in __init__
    self._setup(self._connection_class(host, port, strict))
  File "C:\Python27\lib\httplib.py", line 681, in __init__
    self._set_hostport(host, port)
  File "C:\Python27\lib\httplib.py", line 706, in _set_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: ''

不过如果我使用链接中提到的POST方法,就能正常工作。我的问题是我需要使用GET方法,为什么会出现这些错误呢?

response = urllib.urlopen(URL, parameter)  // this works

1 个回答

3

通过GET请求发送XML文件简直是胡说八道。

应该用POST请求。

撰写回答