2
有下面這段代碼在PCLMVVMCross PCL REST調用工作在Android中沒有的Windows Phone
class CallSAPService : ICallSAPService
{
public string MakeSAPRestCall(string url, string jsondata, string username, string password)
{
string retmessage = "";
try
{
NetworkCredential cr = new NetworkCredential();
cr.UserName = username;
cr.Password = password;
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = cr;
HttpClient httpClient = new HttpClient(handler);
Uri Auri = new Uri(url);
StringContent content = new System.Net.Http.StringContent(jsondata.ToString(), Encoding.UTF8, "application/json");
var result = httpClient.PostAsync(Auri, content).Result;
Task<string> ff = result.Content.ReadAsStringAsync();
retmessage = ff.Result;
//StringContent content2 = new System.Net.Http.StringContent(jsondata.ToString(), Encoding.UTF8, "application/json");
//var httpClient2 = new HttpClient(new HttpClientHandler());
//Task<HttpResponseMessage> response = httpClient2.PostAsync(url, content2);
//response.Result.EnsureSuccessStatusCode();
//var res = response.Result;
// retmessage = res.Content.ReadAsStringAsync().ToString();
}
catch (Exception Ex)
{
retmessage = Ex.Message;
}
return retmessage;
}
}
}
這部作品在Andorid的但不是在Windows Phone的項目。在Windows Phone的項目行: Task<string> ff = result.Content.ReadAsStringAsync();
不出來執行