using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; //本文返回XML格式的数据作为例子 namespace kuaidi { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); //winform界面绑定快递公司 Dictionary<string, string> mydic = new Dictionary<string, string>() { //示例为申通快递和顺丰快递 {"sto","申通快递|申通E物流"}, {"sf","顺丰快递"}, }; txt_SelectKD.ItemsSource = mydic; txt_SelectKD.SelectedValuePath = "Key"; txt_SelectKD.DisplayMemberPath = "Value"; } private void btn_Select_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(this.txt_KuaiDiID.Text)) { string key = “************************”;//你申请的key值 string KuaiDiID = “快递单号” var KuaiDiType = “所选快递公司的”; string RequestURL = "http://v.juhe.cn/exp/index?com="+KuaiDiType+"&no="+KuaiDiID+"&dtype=xml&key="+key+"; Thread thread = new Thread(new ThreadStart(delegate() { this.SubmitRequest(RequestURL); })); thread.Start(); } else { MessageBox.Show("请输入快递单号!", "提示"); } } //提交请求 public string SubmitRequest(string RequestURL) { string result = null; HttpWebResponse response = null; HttpWebRequest request = WebRequest.Create(RequestURL) as HttpWebRequest; request.Method = "GET"; response = (HttpWebResponse)request.GetResponse(); result = ReadHttpResponse(response); return result; } //返回请求 public string ReadHttpResponse(HttpWebResponse response) { string result = string.Empty; var responseStream = response.GetResponseStream(); if (responseStream != null && (response.StatusCode == HttpStatusCode.OK && responseStream.CanRead)) { result = new StreamReader(response.GetResponseStream()).ReadToEnd(); } XMLnode(result); response.Close(); return result; } //解析xml public string XMLnode(string XMLContent) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(XMLContent); XmlNodeList List = xmlDoc.SelectSingleNode("//Root/Data").ChildNodes; foreach (XmlNode xn in List) { var Result = xn.InnerText; this.Dispatcher.Invoke(new Action(() => { this.txt_Result.Items.Add(Result); })); } return "123"; } } }