利用httplib 以http发送post请求网上很多资料。https的处理方式,有所不同,弄不好,报480等错误。
正确的处理方式,上代码:
-
#HTTPS发送POST请求范例
-
import httplib
-
import ssl
-
import socket
-
import base64
-
import simplejson
-
try:
-
receipt = '{"data":1111}'
-
encodedReceipt = base64.b64encode(receipt)
-
param= simplejson.dumps({"receipt-data":encodedReceipt})
-
API_url = "xx.xxx.com"
-
#关键代码开始
-
httpClient = httplib.HTTPSConnection(API_url)
-
sock = socket.create_connection((httpClient.host, httpClient.port))
-
httpClient.sock = ssl.wrap_socket(sock, httpClient.key_file, httpClient.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
-
httpClient.request('POST', "/verifyReceipt/", param)
-
returnData = httpClient.getresponse().read()
-
data = simplejson.loads(returnData)
-
except Exception, e:
-
print e
-
finally:
-
if httpClient:
-
httpClient.close()
附代码:
-
import httplib, ssl, socket
-
-
conn = httplib.HTTPSConnection(URL.hostname)
-
sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)
-
conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
-
conn.request('POST', URL.path + URL.query)
阅读(8433) | 评论(0) | 转发(0) |