此範例為呼叫財政部提供的WebAPI
static void Main(string[] args) {
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
try
{
string url = "https://api.einvoice.nat.gov.tw/PB2CAPIVAN/Carrier/AppGetBarcode";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
NameValueCollection postParams = System.Web.HttpUtility.ParseQueryString(string.Empty);
var nowTicks = (int)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;//時間戳記
var timeStamp = (nowTicks + 10).ToString();
//需要的參數
string myQueryText = string.Format(@"action={0}&appID={1}&phoneNo={2}&timeStamp={3}&uuid={4}&verificationCode={5}&version={6}",
"getBarcode", "申請得到的appID", "自己的手機號碼", timeStamp, "test", "手機條碼驗證碼","1.0");//
string postData = myQueryText;
string result = string.Empty;
using (WebClient webClient = new WebClient())
{
webClient.Encoding = Encoding.UTF8;
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
result = Encoding.UTF8.GetString(webClient.UploadData(url, byteArray));
}
Console.WriteLine(result);//印出結果
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}