国内包裹邮资计算API(澳大利亚邮政)

时间:2014-07-29 17:06:00

标签: c# asp.net json web json.net

我在澳大利亚有一个购物网站。我想在我的网站上使用澳大利亚邮寄方式(asp.net with C#4.0)。我的网站没有任何SSL证书。下面的代码在3个月之前运行良好,但现在这段代码出错了。我的网站托管在Windows共享服务器上。

此URL是api的完整文档。 https://auspost.com.au/devcentre/assets/pdfs/pac-pcs-technical-specification.pdf

我正在关注上述网址(5.3国内包裹邮资计算API),但我不知道我的代码中存在什么问题。

我对JSON了解不多,所以我使用的是XML Pattern。如果你可以通过json做到这一点我没有问题。我想只需要输出。

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <table>
            <tr>
                <td colspan="2" >
               <h1> Australia Post Shipping Charges Calculator</h1>
                   </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMessage" runat="server" EnableViewState="False" 
                        Font-Bold="True"></asp:Label>
                </td>
            </tr>
              <tr>
                <td>
                    From postcode:</td>
                <td>
                   <asp:TextBox ID="txtFromPostCode" runat="server"  MaxLength="4"></asp:TextBox>Ex:3216

                </td>
            </tr>
            <tr>
                <td>
                    To postcode:</td>
                <td>
                     <asp:TextBox ID="txtToPostCode" runat="server"  MaxLength="4"></asp:TextBox>Ex:3217

                </td>
            </tr>
            <tr>
                <td>
                    Weight</td>
                <td>
                     <asp:TextBox ID="txtWeight" runat="server"  MaxLength="4"></asp:TextBox>Ex:5

                </td>
            </tr>
            <tr>
                <td>
                    Service Type</td>
                <td>
                    <asp:DropDownList ID="ddlServiceType" runat="server">
                        <asp:ListItem Value="0">--select service type--</asp:ListItem>
                        <asp:ListItem Value="AUS_PARCEL_REGULAR">Regular Parcel</asp:ListItem>
                        <asp:ListItem Value="AUS_PARCEL_EXPRESS">Express Post Parcel</asp:ListItem>
                        <asp:ListItem Value="AUS_PARCEL_PLATINUM">Express Post Platinum Parcel</asp:ListItem>
                    </asp:DropDownList>

                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnShippingCalculate" runat="server" Text="Shipping Calculate" 
                        onclick="btnShippingCalculate_Click" ValidationGroup="vs" />

                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                    <div style="padding:20px;">

                    <h3>   <%#Eval("service")%></h3>
             <%#Eval("delivery_time")%><br />
            Price :$<%#Eval("total_cost")%></div>
                    </ItemTemplate>
                    </asp:Repeater>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

代码背后:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO;
using System.Net;

public partial class parcelapiDemo : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected string calculateCost(string lngth, string wdth, string hgth, string fpcode, string tpcode, string weght, string service_code)
    {

        string url = "https://test.npe.auspost.com.au/api/postage/parcel/domestic/calculate.xml?";
        url = url + "length=" + HttpUtility.UrlEncode(lngth) + "&width=" + HttpUtility.UrlEncode(wdth) + "&height=" + HttpUtility.UrlEncode(hgth) + "&from_postcode=" + HttpUtility.UrlEncode(fpcode) + "&to_postcode=" + HttpUtility.UrlEncode(tpcode) + "&option_code=&weight=" + HttpUtility.UrlEncode(weght) + "&service_code=" + HttpUtility.UrlEncode(service_code) + "&extra_cover=";
        Uri objURI = new Uri(url);
        HttpWebRequest objwebreq = (HttpWebRequest)WebRequest.Create(objURI);
        objwebreq.ContentType = "text/xml;charset=utf-8;";
        objwebreq.Method = "Get";
        objwebreq.Timeout = 15000;


        objwebreq.Headers.Set("AUTH-KEY", "28744ed5982391881611cca6cf5c2409");//For Localhost


        HttpWebResponse objWebResponse = (HttpWebResponse)objwebreq.GetResponse();
        Stream objStream = objWebResponse.GetResponseStream();
        StreamReader objStreamReader = new StreamReader(objStream);
        return objStreamReader.ReadToEnd();

    }
    protected void btnShippingCalculate_Click(object sender, EventArgs e)
    {


            string xmlresult = calculateCost("10", "10", "10",txtFromPostCode.Text, txtToPostCode.Text,txtWeight.Text, ddlServiceType.SelectedValue);
            DataSet ds = new DataSet();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xmlresult);
            ds.ReadXml(new System.IO.StringReader(doc.OuterXml));
            Repeater1.DataSource = ds;
            Repeater1.DataBind();


    }
}

0 个答案:

没有答案