【发布时间】:2016-02-16 16:01:32
【问题描述】:
我想使用我的 Windows 通用应用程序发出 Json Post 请求。 我可以在我的 Android 和 IOS 中使用它。
public String DoServiceCall()
{
var request = (HttpWebRequest)WebRequest.Create(string.Format("{2}/{0}/{0}ServiceJson.svc/{1}", "Authentication", "Authenticate", "https://....."));
using (MemoryStream ms = new MemoryStream())
{
// create the request object
string requestString = JSONRequest;
byte[] requestData = Encoding.UTF8.GetBytes(requestString);
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.ContentLength = requestData.Length;
request.AllowAutoRedirect = false;
// add known cookies to request (needed for authentication)
CookieContainer requestCookies = new CookieContainer();
foreach (Cookie knownCookie in this._cookieCollection)
{
requestCookies.Add( knownCookie);
}
request.CookieContainer = requestCookies;
//For getting rid of the https Problem
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (Stream stream = request.GetRequestStream())
{
stream.Write(requestData, 0, requestData.Length);
}
// get response data
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return (responseString);
}
}
问题是 Windows Universal 不支持。
request.ContentLength = requestData.Length;
request.AllowAutoRedirect = false;
requestCookies.Add( knownCookie); //with only one Argument
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
也不支持。
request.GetRequestStream())
(HttpWebResponse) request.GetResponse();
但是我可以用异步修复
await request.GetRequestStreamAsync())
(HttpWebResponse)await request.GetResponseAsync();
但如果没有那 4 行,我无法让它在 Windows 上运行。 我只是没有得到任何回应。 是否有让它在 Windows 10 上运行的选项,或者是否有可行的替代方案。
【问题讨论】:
-
试试this答案。
-
试过了,还是没有答案。我想我可能是自定义 SSL Zertifikat 的问题。
-
你没有,检查 HttpClient 类,它可以满足你的所有需求。
标签: c# https xamarin win-universal-app xamarin.forms