c#-来自Windows Phone 8的Http GET请求

此代码适用于Windows窗体:string URI = http://localhost/1/index.php?dsa=232323;string myParameters = ;using (WebClient wc = new WebClient()){wc.Headers[HttpRequestHeader.ContentType] = applicati...

此代码适用于Windows窗体:

string URI = "http://localhost/1/index.php?dsa=232323";
string myParameters = "";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
}

但是我想从Windows Phone 8发送http GET请求.在wp 8中没有方法UploadString()等…

解决方法:

只需使用HttpClient

using(HttpClient hc = new HttpClient())
{
    var response = await hc.PostAsync(url,new StringContent (yourString));
}

对于您的情况,您可以上传FormUrlEncodedContent内容,而不是手动形成上传字符串.

using(HttpClient hc = new HttpClient())
{
    var keyValuePairs = new Dictionary<string,string>();
    // Fill keyValuePairs

    var content = new FormUrlEncodedContent(keyValuePairs);

    var response = await hc.PostAsync(url, content);
}

本文标题为:c#-来自Windows Phone 8的Http GET请求

基础教程推荐