Hi guys,

I have a code that sends sms through clickatell gateway, but now I am getting the NotSupportedException with the message "this stream does not support seek operations". Anyone who has used clickatell gateway for sms successfully please help me.

Please help me eliminate this exception and get my code working.
here is my code:

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR" +
                "1.0.3705;)");
              
                client.QueryString.Add("user", "*****");
                client.QueryString.Add("password", "******");
                client.QueryString.Add("api_id", "******");
                client.QueryString.Add("to", sms);
                client.QueryString.Add("text", "A new tender is uploaded, visit our website");
                string baseurl = "http://api.clickatell.com/http/sendmsg";
                System.IO.Stream data = client.OpenRead(baseurl); //I get the exception after this line
                
                System.IO.StreamReader reader = new System.IO.StreamReader(data);
                string s = reader.ReadToEnd();
                //data.Close();
                //reader.Close();
                return ;

Thanks,:confused:

Dani AI

Generated

Brief diagnosis and practical fixes (for , with the safety note from in mind).

A NotSupportedException coming from an HTTP read usually means the response Stream is forward-only and some code later tried to perform a seek/position/length operation on it. The simplest remedies are to avoid giving non-seekable streams to code that expects seeking, or to convert the response into a seekable buffer first.

Recommended options

  • Use a higher-level method that returns the whole body (no stream): DownloadString or DownloadData on WebClient. That avoids stream semantics entirely.
  • If a seekable Stream is required, copy the response into a MemoryStream and then work off that. Example pattern:
    using (var wc = new WebClient())
    {
      byte[] bytes = wc.DownloadData(url);
      using (var ms = new MemoryStream(bytes))
      using (var reader = new StreamReader(ms))
      {
          string body = reader.ReadToEnd();
      }
    }
  • If using low-level HttpWebRequest/GetResponse, check response.GetResponseStream().CanSeek and, when false, copy to a MemoryStream before passing it to components that call Seek or inspect Length/Position.

Troubleshooting tips

  • Inspect the full stack trace to see which method is triggering the seek call; that shows whether the problem is in the app code or a third-party routine.
  • Search the codebase for explicit Seek, Position or Length usages on the stream you supply.
  • Always URL-encode query/text parameters and wrap network objects in using blocks to avoid resource leaks.

Security note

  • Credentials shown publicly are a real risk (as pointed out). Any exposed account details should be rotated and credentials never posted in public threads.

You should NEVER post usernames/passwords or the like on the web. If you construct the URL from your code sample anyone in the world can with this information send text messages: http://api.clickatell.com/http/sendmsg?user=ditshego&password=khanyala1&api_id=3092895&to=55543651&text=Testing+the+url

You should imediatly change your password for this site before someone else hijacks your account.

commented: Great advice :) +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.