1

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();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • You need help parsing the XML? Show us what you've tried so far and we'll be able to point you to the right direction. – t0mppa Feb 15 '14 at 21:26
  • the string s have the response, from i want to get the id – Luqman Tariq Feb 15 '14 at 21:31
  • Please don't post code in comments (looks ugly & people don't always read all the comments), just edit your question and add it there. – t0mppa Feb 15 '14 at 21:33
  • We were all new here once. :) P.S. You should also use line breaks & indents on your code, else no one is going to read it. The rule of thumb here is that people put about as much trouble into writing good answers as you do in writing a good question. – t0mppa Feb 15 '14 at 21:38

3 Answers3

1

You can use Linq to Xml

var sessionid = XDocument.Parse(s).Descendants("data").First().Value;
L.B
  • 114,136
  • 19
  • 178
  • 224
0

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);
   }

}

Aftab Ahmed
  • 1,727
  • 11
  • 15
0
  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);
scumah
  • 6,273
  • 2
  • 29
  • 44