Question

I'm new to C#. I want to send message from a desktop app using C#, for that I bought an API from a mobile company (Telenor). 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:

<?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).

I tried many methods to bring out the session ID and use it but have got no idea, how to do it. 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();
Was it helpful?

Solution

You can use Linq to Xml

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

OTHER TIPS

First save your file to some path then use this code to get the desired node in 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top