Busy working on Visual Studio 2010 Express for Windows phone and is wanting to call a servlet that returns json from one of my classes.
I have the following method so far:
public Login(string userName, string password){
string servletUrl = "http://172.12.5.35:8080/SomeService/login?u="+userName+"&p="+password;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(servletUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
But for some reason I get an error right by the “GetResponse()” method call saying that there is no such method? Anybody has any ideas? I’ve looked around quite a bit and my code seems right to me?
Edited—————————
public Login(string userName, string password){
string servletUrl = "http://172.12.5.35:8080/SomeService/login?u="+userName+"&p="+password;
using(var client = (IDisposable)new WebClient(servletUrl))
{
string result = client.DownloadString(servletUrl);
{
}
But it seems that there is no DownloadString method for WebClient?
Windows Phone 7 (and Silverlight in general) doesn’t support synchronous IO. If you look at the Silverlight API documentation, you’ll find the compiler is entirely right – there isn’t a
GetResponsemethod. You need to use the asyncBeginGetResponsemethod instead.Alternatively, use
WebClientwhich makes the async part somewhat simpler – and of course C# 5’s async support will make asynchrony much easier in general.EDIT: As noted in comments –
DownloadStringis still synchronous, so not supported in Silverlight. You want the async API, e.g.DownloadStringAsync.