using System.Net;
using System.Text;
using System.Xml;

//設定要傳送內網API的URL及傳送的資料並取得回傳值
string htmlString = "TestData=123456";

//設定要傳送的URL及傳送的資料並取得回傳值
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

//呼叫API(可以是aspx也可以是ashx)
string APIurl = "http://localhost/TestData.aspx";
byte[] response = webClient.UploadData(APIurl, "POST", Encoding.GetEncoding(65001).GetBytes(htmlString));

//將回傳的資料轉成字串
string resultData = Encoding.GetEncoding(65001).GetString(response);

//建立 XML 讀取器
XmlReaderSettings settings = new XmlReaderSettings();

settings.IgnoreComments = true; // 不處理註解

settings.IgnoreWhitespace = true; // 跳過空白
settings.ValidationType = ValidationType.None; // 不驗證任何資料

XmlReader reader = XmlTextReader.Create(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding(65001).GetBytes(resultData)), settings);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(reader);

//API回傳資料 ErrorCode錯誤代碼 ErrorMessage錯誤訊息 DataNo回傳編號
XmlNodeList xmlnl = xmldoc.GetElementsByTagName(GetNonNullString("ErrorCode"));
if (xmlnl.Count > 0)
ErrorCode = xmlnl[0].InnerText;

xmlnl = xmldoc.GetElementsByTagName(GetNonNullString("ErrorMessage"));
if (xmlnl.Count > 0)
ErrorMessage = xmlnl[0].InnerText;

xmlnl = xmldoc.GetElementsByTagName(GetNonNullString("DataNo"));
if (xmlnl.Count > 0)
DataNo = xmlnl[0].InnerText;

 

紅字部份搭配

public string GetNonNullString(object value)
{
if (value == null || value == DBNull.Value || String.IsNullOrEmpty(value.ToString()))
{
return String.Empty;
}
else
{
return value.ToString();
}
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 波利 的頭像
    波利

    沒有魚的魚缸

    波利 發表在 痞客邦 留言(0) 人氣()