我在使用php或python 2.7但不是python 3.2时看到了一些例子。 提前谢谢!
答案 0 :(得分:5)
想出来......
import urllib.request, urllib.error
data ="""
<?xml version="1.0" ?>
<AccessRequest xml:lang="en-US">
<AccessLicenseNumber>XXXXX</AccessLicenseNumber>
<UserId>XXXX</UserId>
<Password>XXXXX</Password>
</AccessRequest>
<?xml version="1.0" ?>
<RatingServiceSelectionRequest xml:lang="en-US">
<Request>
<TransactionReference>
<CustomerContext>Bare Bones Rate Request</CustomerContext>
<XpciVersion>1.0</XpciVersion>
</TransactionReference>
<RequestAction>Rate</RequestAction>
<RequestOption>Rate</RequestOption>
</Request>
<PickupType>
<Code>01</Code>
</PickupType>
<Shipment>
<Shipper>
<Address>
<PostalCode>44111</PostalCode>
<StateProvinceCode>OH</StateProvinceCode>
<CountryCode>US</CountryCode>
</Address>
</Shipper>
<ShipTo>
<Address>
<PostalCode>74403</PostalCode>
<StateProvinceCode>OK</StateProvinceCode>
<CountryCode>US</CountryCode>
<ResidentialAddressIndicator/>
</Address>
</ShipTo>
<ShipFrom>
<Address>
<PostalCode>44023</PostalCode>
<StateProvinceCode>OH</StateProvinceCode>
<City>Chagrin Falls</City>
<CountryCode>US</CountryCode>
</Address>
</ShipFrom>
<Service>
<Code>03</Code>
</Service>
<Package>
<PackagingType>
<Code>02</Code>
</PackagingType>
<Dimensions>
<UnitOfMeasurement>
<Code>IN</Code>
</UnitOfMeasurement>
<Length>8.0</Length>
<Width>8.0</Width>
<Height>8.0</Height>
</Dimensions>
<PackageWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>12.0</Weight>
</PackageWeight>
</Package>
</Shipment>
</RatingServiceSelectionRequest>
"""
try:
httpresq = urllib.request.Request(url="https://wwwcie.ups.com/ups.app/xml/Rate", data=data.encode('utf_8'), headers={'Content-Type': 'application/x-www-form-urlencoded'})
response = urllib.request.urlopen(httpresq)
return_values = response.read()
print(return_values)
except urllib.error.URLError as e:
error = "urllib.error.URLError exception was raised: %s" % e
print(error)