打单及下载面单事例

Function:

 The main function of this example is to purchase an shipping label for  an order in teapplix and download waybill. 

Interface:

 https://api.teapplix.com/api2/PurchaseLabelForOrder

Program development environment

 The development environment of the sample program is Visual studio .NET 2015 C # .

Published Date:

2017/6/12

 The json string for the purchase label is as follows

{
 "TxnId": "IB14950506712553",
 "From": {
 "ProfileId": "1"
 },
 "Packages": [
 {
 "Weight": {
 "Value": 3,
 "Unit": "OZ"
 },
 "Method": "USPS_FIRST_PACKAGE"
 }
 ],
 "Provider": "TEAPPLIX"
 }
Description:
 1, TxnId is the order number
 2, ProfileId need to setup in teapplix Setup-Shipping profile menu
 3, Provider: this example uses the USPS shipping method provided by Teapplix

The organization of the program is shown in the figure

Program interface

 

The .NET console program example is as follows

1、.NET reference is required as follows
2、XXXXXXXXXXXXXXXXXXX on behalf of the APIToken value, you can logon Teapplix in the SETUP-API menu obtained.
The HttpClientHelp.cs program is as follows

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Web;
namespace WebApp.Utility
{
  public class HttpClientHelp
  {
    public static readonly HttpClient _httpClient = null;
    static HttpClientHelp()
    {
      _httpClient = new HttpClient();          
    }
    //This function is to purchase label for an order in Teapplix
    public static async Task<string> PurchaseLabelForOrder()
    {
     var postJson = "{\"TxnId\": \"IB14950506712553\",\"From\": {\"ProfileId\" : \"1\"}";
     postJson=postJson+",\"Packages\": [{\"Weight\": {\"Value\": 3,\"Unit\": \"OZ\"},\"Method\":";
     postJson=postJson+" \"USPS_FIRST_PACKAGE\"}],\"Provider\": \"TEAPPLIX\"}";

     var requestUri = "https://api.teapplix.com/api2/PurchaseLabelForOrder";
     if (requestUri.StartsWith("https"))
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
     HttpContent httpContent = new StringContent(postJson);
     httpContent.Headers.Add("ContentType", "application/json");
     httpContent.Headers.Add("APIToken", "XXXXXXXXXXXXXXXXXXX");
     var response = await _httpClient.PostAsync(requestUri, httpContent);
     response.EnsureSuccessStatusCode();
     return await response.Content.ReadAsStringAsync();
   } 
    //This function is to download the waybill from the return address of purchase label interface
    //The return waybill address can not be downloaded directly in the browser, is a binary stream PDF file
   public static byte[] DownLoadLabel()
   {
    var requestUri = "https://api.teapplix.com/api2/DownloadLabel/apepxu/test2018/pdf";
    if (requestUri.StartsWith("https"))
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    var wc = new System.Net.WebClient(); 
    wc.Headers.Add("ContentType", "application/pdf");
    wc.Headers.Add("APIToken", "XXXXXXXXXXXXXXXXXXXXXXX"); 
 
     byte[] response = wc.DownloadData(requestUri); 
     return response;
   }
  }
}  
The program.cs program is as follows:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.Utility;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PursechaseLabel();
            DownLoadLabel();
        }
        private static void PursechaseLabel()
        {
            var result = HttpClientHelp.PurchaseLabelForOrder().Result;
            Console.Write(result.ToString());
            Console.ReadLine();
            Console.WriteLine("PushlabelForOrder is done");
        }
       private static void DownLoadLabel()
      {
         string fns = @"d:\test.pdf";
         byte[] abc=HttpClientHelp.DownLoadLabel();
         Bytes2File(abc, fns);
      }

       /// <summary>
       /// 将byte数组转换为文件并保存到指定地址
       /// </summary>
       /// <param name="buff">byte数组</param>
       /// <param name="savepath">保存地址</param>
      public static void Bytes2File(byte[] buff, string savepath)
      {
        if (System.IO.File.Exists(savepath))
        {
            System.IO.File.Delete(savepath);
        }
        FileStream fs = new FileStream(savepath, FileMode.CreateNew);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(buff, 0, buff.Length);
        bw.Close();
        fs.Close();
     }
   }
}