xml - 为 FedEx Web 服务创建 HTTP Post 调用

标签 xml http kotlin soap fedex

FedEx Web 服务文档状态:

FedEx 提供纯 XML Web 服务解决方案,您可以使用该解决方案发送交易,而无需使用为 Web 服务提供 SOAP 协议(protocol)支持的工具。

这是他们在文档中使用的示例:

Example 3: HTTP POST
The following HTTP POST example is a valid working example. It is not guaranteed to work for all
programming languages, applications, and host systems:
POST /xml HTTP/1.0
Referrer: YourCompanyNameGoesHere
Host: ws.fedex.com
Port: 443
Accept: image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */*
Content-Type: text/xml
Content-length: %d
Your FedEx Transaction

------------------------

Each line is followed by one new line character except Content-length and the FedEx transaction. Two new
line characters follow the Content-length line. The FedEx transaction has no extra characters. The Content length line should have the length of the FedEx transaction in place of the %d variable.

如果我想在 kotlin 中使用 HTTP post 调用来设置纯 xml 事务,这会带来什么后果?

这是我要发送的 SOAP WSDL。通过传入跟踪号码跟踪货件

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v18="http://fedex.com/ws/track/v18">
   <soapenv:Header/>
   <soapenv:Body>
      <v18:TrackRequest>
         <v18:WebAuthenticationDetail>
            <v18:ParentCredential>
               <v18:Key>INPUT YOUR INFORMATION</v18:Key>
               <v18:Password>INPUT YOUR INFORMATION</v18:Password>
            </v18:ParentCredential>
            <v18:UserCredential>
               <v18:Key>INPUT YOUR INFORMATION</v18:Key>
               <v18:Password>INPUT YOUR INFORMATION</v18:Password>
            </v18:UserCredential>
         </v18:WebAuthenticationDetail>
         <v18:ClientDetail>
            <v18:AccountNumber>INPUT YOUR INFORMATION</v18:AccountNumber>
            <v18:MeterNumber>INPUT YOUR INFORMATION</v18:MeterNumber>
            <v18:Localization>
               <v18:LanguageCode>EN</v18:LanguageCode>
            </v18:Localization>
         </v18:ClientDetail>
         <v18:TransactionDetail>
            <v18:CustomerTransactionId>Track By Number_v18</v18:CustomerTransactionId>
            <v18:Localization>
               <v18:LanguageCode>EN</v18:LanguageCode>
               <v18:LocaleCode>US</v18:LocaleCode>
            </v18:Localization>
         </v18:TransactionDetail>
         <v18:Version>
            <v18:ServiceId>trck</v18:ServiceId>
            <v18:Major>18</v18:Major>
            <v18:Intermediate>0</v18:Intermediate>
            <v18:Minor>0</v18:Minor>
         </v18:Version>
         <v18:SelectionDetails>
            <v18:CarrierCode>FDXE</v18:CarrierCode>
            <v18:PackageIdentifier>
               <v18:Type>TRACKING_NUMBER_OR_DOORTAG</v18:Type>
               <v18:Value>INPUT YOUR INFORMATION</v18:Value>
            </v18:PackageIdentifier>
            <v18:ShipmentAccountNumber/>
            <v18:SecureSpodAccount/>
            <v18:Destination>
               <v18:GeographicCoordinates>rates evertitque aequora</v18:GeographicCoordinates>
            </v18:Destination>
         </v18:SelectionDetails>
      </v18:TrackRequest>
   </soapenv:Body>
</soapenv:Envelope>

最佳答案

我最近成功发送请求并从 Fedex 获得响应。在开发门户上进行初始设置(获取 key 等)后,构建和 XML 类似的内容(示例处理发货请求)。

关键的缺失部分是端点。您将无法使用 SOAP 端点。而是使用 https://wsbeta.fedex.com:443/xml

此外,请更改以下 header - 按照 Fedex 的建议

接受=图像/gif、图像/jpeg、图像/pjpeg、文本/纯文本、文本/html、*/*;

内容类型=文本/xml

祝你好运!!

<?xml version="1.0" encoding="UTF-8"?>
<ProcessShipmentRequest xmlns="http://fedex.com/ws/ship/v26">
    <WebAuthenticationDetail>
        <UserCredential>
            <Key>Input your Key</Key>
            <Password>Input your pwd</Password>
        </UserCredential>
    </WebAuthenticationDetail>
    <ClientDetail>
        <AccountNumber>Input your Account Number</AccountNumber>
        <MeterNumber>Input your meter number</MeterNumber>
    </ClientDetail>
    <!-- <TransactionDetail>
        <CustomerTransactionId>Express Shipment Example</CustomerTransactionId>
    </TransactionDetail> -->
    <Version>
        <ServiceId>ship</ServiceId>
        <Major>26</Major>
        <Intermediate>0</Intermediate>
        <Minor>0</Minor>
    </Version>
    <RequestedShipment>
        <ShipTimestamp>2020-09-01T11:30:40.574Z</ShipTimestamp>
        <DropoffType>REGULAR_PICKUP</DropoffType>
        <ServiceType>FEDEX_GROUND</ServiceType>
        <PackagingType>YOUR_PACKAGING</PackagingType>
        <Shipper>
            <Contact>
                <PersonName>John Doe</PersonName>
                <CompanyName>FedEx</CompanyName>
                <PhoneNumber>9015551234</PhoneNumber>
            </Contact>
            <Address>
                <StreetLines>3610 Hacks Cross Road</StreetLines>
                <StreetLines>First Floor</StreetLines>
                <City>Memphis</City>
                <StateOrProvinceCode>TN</StateOrProvinceCode>
                <PostalCode>38125</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        </Shipper>
        <Recipient>
            <Contact>
                <PersonName>Joe Customer</PersonName>
                <CompanyName>ABC Widget Co</CompanyName>
                <PhoneNumber>3305551234</PhoneNumber>
            </Contact>
            <Address>
                <StreetLines>1234 Main Street</StreetLines>
                <StreetLines>Suite 200</StreetLines>
                <City>Akron</City>
                <StateOrProvinceCode>OH</StateOrProvinceCode>
                <PostalCode>44333</PostalCode>
                <CountryCode>US</CountryCode>
                <Residential>false</Residential>
            </Address>
        </Recipient>
        <ShippingChargesPayment>
            <PaymentType>SENDER</PaymentType>
            <Payor>
                <ResponsibleParty>
                    <AccountNumber>Enter Account Number</AccountNumber>
                    <Contact />
                </ResponsibleParty>
            </Payor>
        </ShippingChargesPayment>
        <LabelSpecification>
            <LabelFormatType>COMMON2D</LabelFormatType>
            <ImageType>PDF</ImageType>
            <LabelStockType>PAPER_4X6</LabelStockType>
        </LabelSpecification>
        <PackageCount>1</PackageCount>
        <RequestedPackageLineItems>
            <SequenceNumber>1</SequenceNumber>
            <Weight>
                <Units>LB</Units>
                <Value>10.0</Value>
            </Weight>
            <Dimensions>
                <Length>5</Length>
                <Width>5</Width>
                <Height>5</Height>
                <Units>IN</Units>
            </Dimensions>
            <CustomerReferences>
                <CustomerReferenceType>CUSTOMER_REFERENCE</CustomerReferenceType>
                <Value>ref1234</Value>
            </CustomerReferences>
        </RequestedPackageLineItems>
    </RequestedShipment>
</ProcessShipmentRequest>

关于xml - 为 FedEx Web 服务创建 HTTP Post 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62523251/

相关文章:

javascript - 此代码返回 301 永久移动,而同一脚本正常工作

rest - REST 仅适用于 Web API 吗?如果不是,那么除了 Web API 之外,还有哪些系统可以遵循 REST?

Kotlin 和有区别的联合(求和类型)

spring-boot - Spring Boot Maven 插件错误 - 运行时发生异常。空 : NullPointerException

generics - 语法 : multiple generic constraints and inheritance, 接口(interface)

Java/Android - https 在 apache http 中不工作

Java JAXB - 如何使用生成的 bean

php - 读取 Soap Response XML 并插入到 mysql 中

rest - 在 RESTful Web 服务中,增量功能应使用什么 HTTP 方法?

Android:动态添加的 TextView 不换行文本