I dod not know what exactly is wrong with your code. Possibly you have to set some properties before issuing the web request. Here is a snippet I used to get timezone information from a web service:
/// <summary>
/// This will call the webservice asynchronousely
/// the call will immediately return
/// The answer on the request is published to all event subscribers
/// </summary>
/// <param name="lat">decimal input for geographic Latitude</param>
/// <param name="lng">decimal input for geographic Longitude</param>
public void startRequest(string lat, string lng)
{
//tzInfos pTZinfos = new tzInfos();
string myURL = @"http://ws.geonames.org/" + _webMethodName;
//HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myURL);
//HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
//string wsdl = "";
_webServiceURI = myURL;
try
{
_httpRequest = (HttpWebRequest)HttpWebRequest.Create(this._webServiceURI + "?lat=" + lat + "&lng=" + lng); // "?WSDL");
_httpRequest.Method = "POST";
_httpRequest.KeepAlive = false;
_httpRequest.ContentType = "application/x-www-form-urlencoded";
//httpRequest.ContentLength = requestData.Length;
_httpRequest.Timeout = 30000;
//async call
IAsyncResult myAsycnCall = this._httpRequest.BeginGetResponse(new AsyncCallback(this.ResponseReceivedHandler), null);
}
catch (WebException wx)
{
System.Diagnostics.Debug.WriteLine("Exception: " + wx.Message);
}
catch (Exception wx)
{
System.Diagnostics.Debug.WriteLine("Exception: " + wx.Message);
}
}
/// <summary>
/// This is the async method for the response to be get from the web service
/// </summary>
/// <param name="result"></param>
private void ResponseReceivedHandler(IAsyncResult result)
{
string wsdl = "";
geonamesTZfields pTZinfos = new geonamesTZfields();
geonamesEventArgs tArg = new geonamesEventArgs();
try
{
HttpWebResponse response = (HttpWebResponse)this._httpRequest.EndGetResponse(result);
System.IO.Stream streamResponse = response.GetResponseStream();
System.IO.StreamReader streamRead = new System.IO.StreamReader(streamResponse);
wsdl = streamRead.ReadToEnd();
System.Diagnostics.Debug.WriteLine("Response: " + wsdl);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
if (wsdl.Length > 0)
{
StringBuilder sb = new StringBuilder(wsdl);
pTZinfos.strCountryCode = xml_helper.xml_helper.getStrSetting(sb, "countryCode");
pTZinfos.strCountryName = xml_helper.xml_helper.getStrSetting(sb, "countryName");
pTZinfos.strTimezoneID = xml_helper.xml_helper.getStrSetting(sb, "timezoneId");
pTZinfos.gmtOffset = xml_helper.xml_helper.getDecimalSetting(sb, "gmtOffset");
pTZinfos.rawOffset = xml_helper.xml_helper.getDecimalSetting(sb, "rawOffset");
pTZinfos.dstOffset = xml_helper.xml_helper.getDecimalSetting(sb, "dstOffset");
pTZinfos.tzTime = xml_helper.xml_helper.getDateTimeSetting(sb, "time");
}
_tzFields = pTZinfos;
//fire the event
tArg.m_myEventArgumentdata = pTZinfos;
tArg.m_myEventArgumentdata.DataIsValid = true;
tArg.m_myEventArgumentdata.strLastError = "no error";
}
catch (WebException wx)
{
tArg.m_myEventArgumentdata.DataIsValid = false;
tArg.m_myEventArgumentdata.strLastError = wx.Message;
}
catch (Exception ex)
{
tArg.m_myEventArgumentdata.DataIsValid = false;
tArg.m_myEventArgumentdata.strLastError = ex.Message;
}
finally
{
this.onDataReceived(tArg); //notify all subscribers of new data arrived
}
}
You see I explicitely set some properties: _httpRequest.Method = "POST"; _httpRequest.KeepAlive = false; _httpRequest.ContentType = "application/x-www-form-urlencoded"; //httpRequest.ContentLength = requestData.Length; _httpRequest.Timeout = 30000; The full code is available by my article at: http://www.hjgode.de/wp/2010/03/04/using-ws-geonames-org-timezone-webservice-without-wsdl/. You may use that and change your web request and use the async call.
You may also try to simple open the web page: http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo in Internet Explorer Mobile on the device. You should get the answer back as:
{"postalCodes":[{"adminCode3":"3203","adminName2":"Wahlkreis St. Gallen","adminName3":"St. Gallen","adminCode2":"1721","adminCode1":"SG","postalCode":"9011","countryCode":"CH","lng":9.399845287638328,"placeName":"St. Gallen","lat":47.414775553450646,"adminName1":"Kanton St. Gallen"},{"adminCode1":"GS","postalCode":"9011","countryCode":"HU","lng":17.781944437499998,"placeName":"Győr","lat":47.607638900000005,"adminName1":"Győr-Moson-Sopron"},{"adminName2":"Tromsø","adminCode2":"1902","adminCode1":"19","postalCode":"9011","countryCode":"NO","lng":18.95508,"placeName":"Tromsø","ISO3166-2":"10","lat":69.6489,"adminName1":"Troms"},{"postalCode":"L-9011","countryCode":"LU","lng":6.1041667,"placeName":"Ettelbruck","lat":49.8475},{"adminCode1":"Z","postalCode":"9011","countryCode":"AR","lng":-67.5333333,"placeName":"CALETA OLIVIA","lat":-46.4333333,"adminName1":"SANTA CRUZ"},{"adminCode1":"Z","postalCode":"9011","countryCode":"AR","lng":-67.5333333,"placeName":"EL MANGRULLO","lat":-46.4333333,"adminName1":"SANTA CRUZ"},{"adminCode1":"Z","postalCode":"9011","countryCode":"AR","lng":-67.5333333,"placeName":"CAÑADON ESTHER","lat":-46.4333333,"adminName1":"SANTA CRUZ"},{"adminCode1":"Z","postalCode":"9011","countryCode":"AR","lng":-67.5333333,"placeName":"BAHIA LANGARA","lat":-46.4333333,"adminName1":"SANTA CRUZ"},{"postalCode":"9011","countryCode":"NZ","lng":170.47198,"placeName":"Mornington","lat":-45.87922},{"postalCode":"9011","countryCode":"PH","lng":124.7325,"placeName":"Balinguan","lat":9.050317457142857}]}
If that does not come in Internet Explorer Mobile, your connection is setup incorrectlyLook for WMDC connection settings adn 'This computer connects to'. Switching from Internet to Automatic may help. I assume you have docked the Emulator you use and WMDC or ActiveSync shows a connection with the emulator.