在Xamarin中使用PCL方法構建了一個應用程序,並且使用標準HTTP使其100%工作,現在我已將遠程測試服務器更改爲使用帶有自簽名證書的SSL。WebRequest在切換到SSL時返回404
該應用程序聯繫自定義API以登錄到服務器並查詢特定數據。
我已經改變了應用程序在SSL看看現在開始就得到了驗證不工作或一些錯誤,但關閉SSL相關的錯誤使用測試:
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
在我的AppDelegate文件
FinishedLaunching方法,克服了那個錯誤。
當我嘗試對給定的URL執行登錄POST時,現在出現404 /協議錯誤。
我爲我的RESTful調用使用HttpWebRequest,並且如果我更改回純樸的http,這可以正常工作。
不知道爲什麼,但一些文章建議使用ModernHttpClient,我做了。我導入了組件(還使用NuGet添加了該組件)無濟於事。
我在接觸SSL服務器時是否缺少應該在與httpwebresponse相關的代碼中配置的其他內容,或者此組件是否無法與SSL服務器通話?
我的登錄功能如下(相依代碼移除/混淆):
public JsonUser postLogin(string csrfToken, string partnerId, string username, string password){
string userEndPoint = SingletonAppSettngs.Instance().apiEndPoint;
userEndPoint = userEndPoint.Replace ("druid/", "");
var request = WebRequest.CreateHttp(string.Format(this.apiBaseUrl + userEndPoint + @"user/login.json"));
// Request header collection set up
request.ContentType = "application/json";
request.Headers.Add ("X-CSRF-Token", csrfToken);
// Add other configs
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json_body_content = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}";
streamWriter.Write(json_body_content);
streamWriter.Flush();
streamWriter.Close();
}
try{
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader (httpResponse.GetResponseStream())) {
var content = reader.ReadToEnd();
content = content.Replace ("[],", "null,");
content = content.Replace ("[]", "null");
if (content == null) {
throw new Exception ("request_post_login - content is NULL");
} else {
JsonSerializerSettings jss = new JsonSerializerSettings();
jss.NullValueHandling = NullValueHandling.Ignore;
JsonUser deserializedUser = JsonConvert.DeserializeObject<JsonUser>(content, jss);
if(content.Contains ("Hire company admin user")){
deserializedUser.user.roles.__invalid_name__5 = "Hire company admin user";
deserializedUser.user.roles.__invalid_name__2 = "authenticated user";
}
return deserializedUser;
}
}
}catch(Exception httpEx){
Console.WriteLine ("httpEx Exception: " + httpEx.Message);
Console.WriteLine ("httpEx Inner Exception: " + httpEx.InnerException.Message);
JsonUser JsonUserError = new JsonUser();
JsonUserError.ErrorMessage = "Error occured: " + httpEx.Message;
return JsonUserError;
}
}