简体   繁体   English

来自第三方的网络响应,使用其给定的API,C#

[英]webresponse from 3rd party, using their given API, C#

I'm new to C#. 我是C#的新手。 I want to send message from a desktop app using C#, for that I bought an API from a mobile company (Telenor). 我想使用C#从桌面应用程序发送消息,为此我从移动公司(Telenor)购买了API。 According to their documents first I'll have to get authentication ID by sending request to this URL ( https://telenorcsms.com.pk:27677/corporate_sms2/api/auth.jsp?msisdn=xxxx&password=xxx ) and it gives me response in XML format like this: 首先根据他们的文档,我必须通过向该URL发送请求来获取身份验证ID( https://telenorcsms.com.pk:27677/corporate_sms2/api/auth.jsp?msisdn=xxxx&password=xxx ),它会给我XML格式的响应如下:

<?xml version="1.0" encoding="UTF-8" ?>
<corpsms>
  <command>Auth_request</command>
  <data>Session ID</data>
  <response>OK</response>
</corpsms>

Now I need the session ID which is in <data> node, to use further for sending message like ( https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=xxxx&to=923xxxxxxxxx,923xxxxxxxxx,923xxxxxxxxx&text=xxxx&mask=xxxx ). 现在,我需要位于<data>节点中的会话ID,以便进一步用于发送消息,例如( https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=xxxx&to=923xxxxxxxxx,923xxxxxxxxx, 923xxxxxxxxx&text = xxxx&mask = xxxx )。

I tried many methods to bring out the session ID and use it but have got no idea, how to do it. 我尝试了许多方法来获取会话ID并使用它,但不知道如何执行。 its my code: 它是我的代码:

WebClient client = new WebClient ();
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead ("https://telenorcsms.com.pk:27677/corporate_sms2/api/auth.jsp?msisdn=xxxx&password=xxx");
StreamReader reader = new StreamReader (data);
StreamReader objreadr = new StreamReader(data);
string s = reader.ReadToEnd();

您可以使用Linq到Xml

var sessionid = XDocument.Parse(s).Descendants("data").First().Value;

First save your file to some path then use this code to get the desired node in xml. 首先将文件保存到某个路径,然后使用此代码在xml中获取所需的节点。

public void Load()
{
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    XmlDocument xmldoc = new XmlDocument();
    XmlNodeList xmlnode;

   xmldoc.Load(fs);
   xmlnode = xmldoc.GetElementsByTagName("corpsms");

   for (int i = 0; i < xmlnode.Count; i++)
   {
       string str = string.Format("ID: {0}\r\nName:{0}", xmlnode[i].ChildNodes.Item(0).InnerText, xmlnode[i].ChildNodes.Item(1).InnerText);//Your Data will exist at node 1
       MessageBox.Show(str);
   }

} }

  var url = @"https://example.com/api/auth.jsp";
            var nvc = new NameValueCollection();
            nvc.Add("msisdn", "xxxxxxxxxxxx");
            nvc.Add("password", "xxxx");
            var client = new System.Net.WebClient();
            var data = client.UploadValues(url, nvc);
            var res = System.Text.Encoding.ASCII.GetString(data);
            string GetResponse = res.ToString();
            string sessionid = XDocument.Parse(res).Descendants("data").First().Value;
            url = @"https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp";
            nvc = new NameValueCollection();
            nvc.Add("msisdn", "xxxxxxxxx");
            nvc.Add("session_id",sessionid);
            nvc.Add("to", textBox1.Text);
            nvc.Add("text",textBox2.Text);
             data = client.UploadValues(url, nvc);
             res = System.Text.Encoding.ASCII.GetString(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM