如何調用其他網站提供的介面

來源:互聯網
上載者:User

標籤:style   code   http   ext   get   資料   

這幾天百度的結果,總結一下舉例說明:某網站提供傳送簡訊介面業務介面:SERVICE_URL變數 參數:username,pwd,mobiles,content返回:<ReturnInfo>resultcode</ReturnInfo>
1、構建發送的xml或者其他形式的請求string postData = string.Format("ActionCode=Sms01&UserName=mine&Pwd=123&Mobiles={0}&Content={1}", strMobiles, strMessage+ "【客服中心】");並把請求轉換成位元組格式byte[] data = Encoding.Default.GetBytes(postData);2、 用HttpWebRequest來發送建立請求HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(SERVICE_URL);3、向請求中寫入請求的相關屬性,也就是請求的頭部格式(要求方法,版本類型,請求的字元編碼,回應時間等)            myrequest.Method = "POST";            myrequest.ContentType = "application/x-www-form-urlencoded";            myrequest.ContentLength = data.Length;4、 向請求中寫入請求資訊   using (Stream datasteam = myrequest.GetRequestStream())            {                datasteam.Write(data, 0, data.Length);                datasteam.Close();            }5、得到響應請求 HttpWebResponse myResponse = myrequest.GetResponse() as HttpWebResponse;6、讀取響應中的資料   HttpWebResponse myResponse = myrequest.GetResponse() as HttpWebResponse;            using (StreamReader reader = new StreamReader(                                                   myResponse.GetResponseStream()                                                 , System.Text.Encoding.GetEncoding("utf-8")))            {                response = reader.ReadToEnd();                xmlDoc.LoadXml(response);                reader.Close();           }一般從網站返回的是一段xml格式的字串7、把該字串轉換成xml格式,並取出當中的相關資料XmlDocument xmlDoc = new System.Xml.XmlDocument();string result = xmlDoc.SelectSingleNode("ReturnInfo").InnerText;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.