将代码c#sync httprequest转换为异步

时间:2012-06-08 01:07:26

标签: windows-phone-7 c#-4.0 silverlight-4.0 asynchronous

我有这个代码的问题,我需要将这个Json httprequest转换为异步方法,以便在WP7 C#应用程序中使用它,这里是代码,我真的需要这方面的帮助。

它在以下语句中显示错误:

1)myReq.ContentLength = postData.Length; - > ContentLength不包含此类方法

2)webresponse =(HttpWebResponse)myReq.GetResponse(); - >是一种Sync方法,不包含在httpresponse metod

3)StreamWriter stOut = new StreamWriter(myReq.GetRequestStream(),System.Text.Encoding.ASCII); - >是一种Sync方法,不包含在httpresponse metod

4)GoogleCell cell = JsonConvert.DeserializeObject(Response); - >方法重载或丢失转换....转换是在第3错误。

  /// internal google cell info to post

    public GoogleCell GetCellInfo(string lac, string mnc, string mcc, string cellID)
    {

        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://www.google.com/loc/json");
        myReq.Method = "POST";
        myReq.ContentType = "application/jsonrequest";
        string postData = "{\"cell_towers\": [{\"location_area_code\": \"" + lac + "\", \"mobile_network_code\": \"" + mnc + "\", \"cell_id\": \"" + cellID + "\", \"mobile_country_code\": \"" + mcc + "\"}], \"version\": \"1.1.0\", \"request_address\": \"true\"}";
        myReq.ContentLength = postData.Length;

        StreamWriter stOut = new StreamWriter(myReq.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(postData);
        stOut.Close();

        HttpWebResponse webresponse;
        webresponse = (HttpWebResponse)myReq.GetResponse();
        Encoding enc = System.Text.Encoding.UTF8;
        StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);

        string Response = loResponseStream.ReadToEnd();
        loResponseStream.Close();
        webresponse.Close();

        GoogleCell cell = JsonConvert.DeserializeObject(Response);
        return cell;


        }
    }

1 个答案:

答案 0 :(得分:1)

在WCF中转换完整的类仍然有错误

JsonConvert.DeserializeObject(响应);