Click to See Complete Forum and Search --> : WebClient class in compact framework?


GeeZuZz
November 25th, 2005, 09:40 AM
I'm trying to "port" a windows application to my Pocket PC, but have some problems...

This is the code i'm trying to use:


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;)");
client.QueryString.Add("user", "....");
client.QueryString.Add("password", "...");
client.QueryString.Add("to", to);
client.QueryString.Add("from", from);
client.QueryString.Add("text", message);

string baseurl = "http://sendmessage...";

Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();



Appearantly, neither Webclient or Streamreader classes are available in the compact framework.

Does anyone want to fix the code above to run on Pocket PC - or give me some hints? :P

Vantskruv
June 24th, 2006, 06:50 PM
I'm having the same problem. I've installed the Windows Mobile 5.0 SDK and created a Visual C#-project for a Windows Mobile 5.0 mobile, supporting .NET Compact Framework 2.0. Are there any other ways to download a file from internet, or do I have to install som new external references in Visual Studio(if, where from?)

Vantskruv
June 26th, 2006, 02:33 AM
I'm updating this thread because I got a solution for this.
Instead of using the WebClient class, which seems not to be implented in Pocket PC programming enviroment, you can use the HttpWebRequest/FileWebRequest, or mainly the WebRequest class.

Example code from my program:

try
{
String forecastAdress = "http://www.weather.com/stockholm..html"

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(forecastAdress);
httpRequest.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

System.IO.Stream dataStream = httpResponse.GetResponseStream();

System.IO.StreamReader streamReader = new System.IO.StreamReader(dataStream);

String forecastData = streamReader.ReadToEnd();

streamReader.Close();
httpResponse.Close();
}//try
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return false;
}//catch


Go to the MSDN-reference for further information of the classes.

/Jompa :)