发出 HTTP POST 请求

发布于 2024-09-18 00:35:24 字数 2329 浏览 4 评论 0原文

我正在尝试发出 POST 请求来检索有关书籍的信息。 以下是返回 HTTP 代码的代码: 302,已移动

import httplib, urllib
params = urllib.urlencode({
    'isbn' : '9780131185838',
    'catalogId' : '10001',
    'schoolStoreId' : '15828',
    'search' : 'Search'
    })
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = httplib.HTTPConnection("bkstr.com:80")
conn.request("POST", "/webapp/wcs/stores/servlet/BuybackSearch",
             params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()

当我从浏览器尝试此页面时: http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackMaterialsView?langId=-1&catalogId=10001& storeId=10051&schoolStoreId=15828 ,它有效。我的代码中缺少什么?

编辑: 这是我调用 print response.msg 时得到的结果

302 Moved Date: Tue, 07 Sep 2010 16:54:29 GMT
Vary: Host,Accept-Encoding,User-Agent
Location: http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch
X-UA-Compatible: IE=EmulateIE7
Content-Length: 0
Content-Type: text/plain; charset=utf-8

似乎该位置指向我首先尝试访问的同一 URL?

EDIT2:

我已经尝试按照此处的建议使用 urllib2 。这是代码:

import urllib, urllib2

url = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch'
values = {'isbn' : '9780131185838',
          'catalogId' : '10001',
          'schoolStoreId' : '15828',
          'search' : 'Search' }


data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
print response.geturl()
print response.info()
the_page = response.read()
print the_page

这是输出:

http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch
Date: Tue, 07 Sep 2010 16:58:35 GMT
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=0001REjqgX2axkzlR6SvIJlgJkt:1311s25dm; Path=/
Vary: Accept-Encoding,User-Agent
X-UA-Compatible: IE=EmulateIE7
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en-US
Set-Cookie: TSde3575=225ec58bcb0fdddfad7332c2816f1f152224db2f71e1b0474c866f3b; Path=/

I'm trying to make a POST request to retrieve information about a book.
Here is the code that returns HTTP code: 302, Moved

import httplib, urllib
params = urllib.urlencode({
    'isbn' : '9780131185838',
    'catalogId' : '10001',
    'schoolStoreId' : '15828',
    'search' : 'Search'
    })
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = httplib.HTTPConnection("bkstr.com:80")
conn.request("POST", "/webapp/wcs/stores/servlet/BuybackSearch",
             params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()

When I try from a browser, from this page: http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackMaterialsView?langId=-1&catalogId=10001&storeId=10051&schoolStoreId=15828 , it works. What am I missing in my code?

EDIT:
Here's what I get when I call print response.msg

302 Moved Date: Tue, 07 Sep 2010 16:54:29 GMT
Vary: Host,Accept-Encoding,User-Agent
Location: http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch
X-UA-Compatible: IE=EmulateIE7
Content-Length: 0
Content-Type: text/plain; charset=utf-8

Seems that the location points to the same url I'm trying to access in the first place?

EDIT2:

I've tried using urllib2 as suggested here. Here is the code:

import urllib, urllib2

url = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch'
values = {'isbn' : '9780131185838',
          'catalogId' : '10001',
          'schoolStoreId' : '15828',
          'search' : 'Search' }


data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
print response.geturl()
print response.info()
the_page = response.read()
print the_page

And here is the output:

http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch
Date: Tue, 07 Sep 2010 16:58:35 GMT
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=0001REjqgX2axkzlR6SvIJlgJkt:1311s25dm; Path=/
Vary: Accept-Encoding,User-Agent
X-UA-Compatible: IE=EmulateIE7
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en-US
Set-Cookie: TSde3575=225ec58bcb0fdddfad7332c2816f1f152224db2f71e1b0474c866f3b; Path=/

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

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

发布评论

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

评论(3

不奢求什么 2024-09-25 00:35:24

他们的服务器似乎希望您获取正确的 cookie。这有效:

import urllib, urllib2, cookielib

cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)

# acquire cookie
url_1 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackMaterialsView?langId=-1&catalogId=10001&storeId=10051&schoolStoreId=15828'
req = urllib2.Request(url_1)
rsp = urllib2.urlopen(req)

# do POST
url_2 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch'
values = dict(isbn='9780131185838', schoolStoreId='15828', catalogId='10001')
data = urllib.urlencode(values)
req = urllib2.Request(url_2, data)
rsp = urllib2.urlopen(req)
content = rsp.read()

# print result
import re
pat = re.compile('Title:.*')
print pat.search(content).group()

# OUTPUT: Title:  Statics & Strength of Materials for Arch (w/CD)<br />

Their server seems to want you to acquire the proper cookie. This works:

import urllib, urllib2, cookielib

cookie_jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)

# acquire cookie
url_1 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackMaterialsView?langId=-1&catalogId=10001&storeId=10051&schoolStoreId=15828'
req = urllib2.Request(url_1)
rsp = urllib2.urlopen(req)

# do POST
url_2 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch'
values = dict(isbn='9780131185838', schoolStoreId='15828', catalogId='10001')
data = urllib.urlencode(values)
req = urllib2.Request(url_2, data)
rsp = urllib2.urlopen(req)
content = rsp.read()

# print result
import re
pat = re.compile('Title:.*')
print pat.search(content).group()

# OUTPUT: Title:  Statics & Strength of Materials for Arch (w/CD)<br />
沉睡月亮 2024-09-25 00:35:24

您可能想使用 urllib2 模块,该模块应该 更好地处理重定向。以下是使用 urllib2 的 POSTING 的示例。

You might want to use the urllib2 module which should handle redirects better. Here's an example of POSTING with urllib2.

七色彩虹 2024-09-25 00:35:24
  1. 也许这就是浏览器得到的,您只需遵循302重定向。

  2. 如果所有其他方法都失败,您可以使用 FireBug、tcpdump 或wireshark 监视 Firefox 和 Web 服务器之间的对话,并查看哪些 HTTP 标头不同。可能只是 User Agent: 标头。

  1. Perhaps that's what the browser gets, and you'll just have to follow the 302 redirect.

  2. If all else fails, you can monitor the dialogue between Firefox and the Web Server using FireBug or tcpdump or wireshark, and see which HTTP headers are different. Possibly it's just the User Agent: header.

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