(WebResponse.GetResponseStream()).ReadToEnd() Exception

G

Guest

Hi, i was interested in sending SMSs from my PDA via an SMS Gateway when the PDA is connected to the Internet (Cradle/Bluetooth). I have wrote the following Class which works fine on the .Net Framework, but rases an Exception on the Compact Framework. It's not just this method, but all methods which read from this Stream. The exception is that the chunk length was not valid.

Here is the code:

--------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Net;
using System.IO;

namespace Clickatell_Web_API
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class HTTPConnection
{

private String Username;
private String Password;
private String API_ID;

public bool Initialized = false;
private String SessionID = null;
private HttpWebRequest webRequest;
private HttpWebResponse webResponse;

public HTTPConnection()
{
}

public HTTPConnection(String Username, String Password, String API_ID)
{
this.Username = Username;
this.Password = Password;
this.API_ID = API_ID;
Initialized = Initialize();
}

public bool Initialize()
{
if ((Username != null) &&
(Password != null) &&
(API_ID != null))
{
StreamWriter streamWriter;
StreamReader streamReader;
String responseString;
char[] seperator;
String[] responseArray;

string Clickatell_Auth_URI = "http://api.clickatell.com/http/auth";
string Auth_Query = "user=" + Username +
"&password=" + Password + "&api_id=" + API_ID;
int Auth_Query_Len = Auth_Query.Length;

webRequest = (System.Net.HttpWebRequest)
System.Net.WebRequest.Create(Clickatell_Auth_URI);

webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded; charset=\"utf-8\"";
webRequest.ContentLength = Auth_Query_Len;

streamWriter = new System.IO.StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(Auth_Query);
streamWriter.Close();

webResponse =
(System.Net.HttpWebResponse) webRequest.GetResponse();

streamReader = new
System.IO.StreamReader(webResponse.GetResponseStream());
responseString = streamReader.ReadToEnd();
Console.WriteLine(webResponse.StatusCode);
Console.WriteLine(responseString);
Console.ReadLine();
seperator = new char[1];
seperator[0] = ':';
responseArray = responseString.Split(seperator);

if (responseArray[0].Trim() == "OK")
{
SessionID = responseArray[1].Trim();
Initialized = true;
return true;
}
}

return false;
}

public bool SendMessage(String To, String Message)
{
return SendMessage(null, To, Message);
}

public bool SendMessage(String From, String To, String Message)
{
if (Initialized)
{
StreamWriter streamWriter;
StreamReader streamReader;
String responseString;
char[] seperator;
String[] responseArray;

string Clickatell_SendMsg_URI = "http://api.clickatell.com/http/sendmsg";

string SendMsg_Query;
if (From == null)
SendMsg_Query = "session_id=" + SessionID +
"&from=" + From + "&to=" + To + "&text=" + Message;
else
SendMsg_Query = "session_id=" + SessionID +
"&to=" + To + "&text=" + Message;

int SendMsg_Query_Len = SendMsg_Query.Length;

webRequest = (System.Net.HttpWebRequest)
System.Net.WebRequest.Create(Clickatell_SendMsg_URI);

webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded; charset=\"utf-8\"";
webRequest.ContentLength = SendMsg_Query_Len;

streamWriter = new System.IO.StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(SendMsg_Query);
streamWriter.Close();

webResponse =
(System.Net.HttpWebResponse) webRequest.GetResponse();

streamReader = new
System.IO.StreamReader(webResponse.GetResponseStream());
responseString = streamReader.ReadToEnd();
Console.WriteLine(webResponse.StatusCode);
Console.WriteLine(responseString);
Console.ReadLine();
seperator = new char[1];
seperator[0] = ':';
responseArray = responseString.Split(seperator);

if (responseArray[0].Trim() == "ID")
{
return true;
}
}
return false;
}

}
}
--------------------------------------------------------------------------------------------------------------------------------------------
 
S

Sandeep Prabhakar [MSFT]

Do you have network traces of what the server is sending back to the
client? I cannot verify this since I don't have the credentials to login to
the server url that you have provided.

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top