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



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

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

1) myReq.ContentLength = postData.Length;--> ContentLength 不包含这种类型的方法

2) webresponse = (HttpWebResponse)myReq.GetResponse();--> 是一个同步方法,不包含在 httpresponse metod 中

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

4) GoogleCell cell = JsonConvert.DeserializeObject(Response);-->方法重载或缺少转换。转换是第三个错误。

  /// 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;

        }
    }

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

JsonConvert.DeserializeObject(Response);

最新更新