You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
7.8 KiB
C#
204 lines
7.8 KiB
C#
using APIForCustomer.BE;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Xml;
|
|
|
|
namespace APIForCustomer.Utility
|
|
{
|
|
public class RequestUtility
|
|
{
|
|
public static RequestEntity GenerateRequestEntity()
|
|
{
|
|
RequestEntity requestEntity = new RequestEntity();
|
|
requestEntity.j_company = "j_company value";
|
|
requestEntity.j_contact = "j_contact value";
|
|
requestEntity.j_tel = "111";
|
|
requestEntity.j_mobile = "112";
|
|
requestEntity.j_country = "CN";
|
|
requestEntity.j_province = "j_province value";
|
|
requestEntity.j_city = "j_city value";
|
|
requestEntity.j_post_code = "113";
|
|
requestEntity.j_address = "j_address value";
|
|
requestEntity.d_company = "d_company value";
|
|
requestEntity.d_contact = "d_contact value";
|
|
requestEntity.d_tel = "221";
|
|
requestEntity.d_mobile = "222";
|
|
requestEntity.d_country = "GB";
|
|
requestEntity.d_province = "d_province value";
|
|
requestEntity.d_city = "d_city value";
|
|
requestEntity.d_post_code = "223";
|
|
requestEntity.d_address = "d_address value";
|
|
requestEntity.orderid = "YGC20141204F";
|
|
requestEntity.express_type = "A1";
|
|
requestEntity.parcel_quantity = "10";
|
|
requestEntity.cargo_total_weight = "20";
|
|
requestEntity.returnsign = "Y";
|
|
|
|
DeclareInvoice declareInvoice = new DeclareInvoice();
|
|
declareInvoice.name = "name";
|
|
declareInvoice.ename = "ename";
|
|
declareInvoice.hscode = "123";
|
|
declareInvoice.count = "1";
|
|
declareInvoice.unit = "PCE";
|
|
declareInvoice.weight = "2";
|
|
declareInvoice.amount = "3";
|
|
requestEntity.DeclareInvoiceList.Add(declareInvoice);
|
|
|
|
declareInvoice = new DeclareInvoice();
|
|
declareInvoice.name = "name2";
|
|
declareInvoice.ename = "ename2";
|
|
declareInvoice.hscode = "112233";
|
|
declareInvoice.count = "11";
|
|
declareInvoice.unit = "PCE";
|
|
declareInvoice.weight = "22";
|
|
declareInvoice.amount = "33";
|
|
requestEntity.DeclareInvoiceList.Add(declareInvoice);
|
|
|
|
return requestEntity;
|
|
}
|
|
|
|
public static string ConvertRequestEntityToXML(RequestEntity requestEntity)
|
|
{
|
|
try
|
|
{
|
|
XmlDocument xmlDoc = new XmlDocument();
|
|
|
|
//Request Node
|
|
XmlElement root = xmlDoc.CreateElement("Request");
|
|
xmlDoc.AppendChild(root);
|
|
root.SetAttribute("service", "OrderService");
|
|
root.SetAttribute("lang", "zh-CN");
|
|
|
|
//Head Node
|
|
XmlNode headNode = xmlDoc.CreateElement("Head");
|
|
headNode.InnerText = ConfigurationManager.AppSettings["CUSTOMER_ID"].ToString().Trim();
|
|
root.AppendChild(headNode);
|
|
|
|
//Body Node
|
|
XmlNode bodyNode = xmlDoc.CreateElement("Body");
|
|
bodyNode.InnerText = string.Empty;
|
|
root.AppendChild(bodyNode);
|
|
|
|
//Order Node
|
|
XmlNode orderNode = xmlDoc.CreateElement("Order");
|
|
orderNode.InnerText = string.Empty;
|
|
bodyNode.AppendChild(orderNode);
|
|
|
|
//Order Node Attribute
|
|
List<string> propertyNameList = new List<string>();
|
|
PropertyInfo[] propertys = requestEntity.GetType().GetProperties();
|
|
foreach (PropertyInfo pi in propertys)
|
|
{
|
|
if (pi.Name == "DeclareInvoiceList")
|
|
{
|
|
foreach(DeclareInvoice declareInvoice in requestEntity.DeclareInvoiceList)
|
|
{
|
|
XmlNode cargoNode = xmlDoc.CreateElement("Cargo");
|
|
orderNode.AppendChild(cargoNode);
|
|
PropertyInfo[] propertysCargo = declareInvoice.GetType().GetProperties();
|
|
foreach (PropertyInfo piCargo in propertysCargo)
|
|
{
|
|
XmlAttribute attribute = xmlDoc.CreateAttribute(piCargo.Name);
|
|
attribute.Value = piCargo.GetValue(declareInvoice, null).ToString().Trim();
|
|
cargoNode.Attributes.Append(attribute);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
XmlAttribute attribute = xmlDoc.CreateAttribute(pi.Name);
|
|
attribute.Value = pi.GetValue(requestEntity, null).ToString().Trim();
|
|
orderNode.Attributes.Append(attribute);
|
|
}
|
|
}
|
|
return xmlDoc.InnerXml;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
private static string QuotationMarks(string str)
|
|
{
|
|
return "\"" + str + "\"";
|
|
}
|
|
|
|
public static string MD5Base64(string input)
|
|
{
|
|
string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "MD5");
|
|
byte[] by = System.Text.Encoding.UTF8.GetBytes(pwd);
|
|
return Convert.ToBase64String(by);
|
|
}
|
|
|
|
public static string SubmitRequest(string xml)
|
|
{
|
|
try
|
|
{
|
|
string check_word = ConfigurationManager.AppSettings["CHECK_WORD"].ToString().Trim();
|
|
string verifyCode = MD5Base64(xml + check_word); //example format: ODA4RUExMzFDRDRFM0ZFOThFOTUwQjAyNDgyOERGNjQ=
|
|
WebReference.SfexpressServiceImplService service = new WebReference.SfexpressServiceImplService();
|
|
string response = service.sfexpressService(xml, verifyCode);
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
|
|
public static string SubmitRequest(RequestEntity requestEntity)
|
|
{
|
|
try
|
|
{
|
|
string xml = ConvertRequestEntityToXML(requestEntity);
|
|
return SubmitRequest(xml);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
|
|
public static string DemoMethod()
|
|
{
|
|
try
|
|
{
|
|
RequestEntity requestEntity = GenerateRequestEntity();
|
|
return SubmitRequest(requestEntity);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
public string sendpost(string URL)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
|
|
request.Method = "POST";
|
|
request.ContentType = "text/html";
|
|
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)";
|
|
request.Headers.Add("Accept-Language: zh-CN");
|
|
request.Headers["Pragma"] = "no-cache"; //禁用缓存
|
|
request.Headers["Cache-Control"] = "no-cache"; //禁用缓存
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
Stream myResponseStream = response.GetResponseStream();
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
string josn = myStreamReader.ReadToEnd();
|
|
|
|
myStreamReader.Close();
|
|
myResponseStream.Close();
|
|
response.Close();
|
|
return josn;
|
|
}
|
|
}
|
|
}
|