Hola comunidad tengo problemas para trabajar con el service layer específicamente con el método PATCH me retorna el siguiente error
the remote server returned an error (401) unauthorized.
El código que utilizo es el siguiente
static ConexionSAP Conexion(string DB)
{
string data = "{\"CompanyDB\": \"" + DB + "\", \"UserName\": \"Desarrollo\", \"Password\": \"S4pb1234\"}";
var WebReq = (HttpWebRequest)WebRequest.Create("https://localhost:50000/b1s/v1/Login");
WebReq.ContentType = "application/json;odata=minimalmetadata;charset=utf8";
WebReq.Method = "POST";
WebReq.KeepAlive = true;
WebReq.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
WebReq.Accept = "application/json;odata=minimalmetadata";
WebReq.ServicePoint.Expect100Continue = false;
WebReq.AllowAutoRedirect = true;
WebReq.Timeout = 10000000;
using (var streamWriter = new StreamWriter(WebReq.GetRequestStream()))
{ streamWriter.Write(data); }
var httpResponse = (HttpWebResponse)WebReq.GetResponse();
ConexionSAP obj = null;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
obj = JsonConvert.DeserializeObject<ConexionSAP>(result);
}
return obj;
}
static void ActualizarSolicitud(ConexionSAP obj)
{
var url = "https://localhost:50000/b1s/v1/PurchaseRequests(3403)";
var WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.ContentType = "application/json;odata=minimalmetadata;charset=utf8";
WebReq.Method = "PATCH";
WebReq.KeepAlive = true;
WebReq.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
WebReq.Accept = "application/json;odata=minimalmetadata";
WebReq.ServicePoint.Expect100Continue = false;
WebReq.AllowAutoRedirect = true;
WebReq.Timeout = 10000000;
var data = "{\"Comments\": \"Prueba desde C 2\" }";
using (var streamWriter = new StreamWriter(WebReq.GetRequestStream()))
{
streamWriter.Write(data);
}
CookieContainer cookies = new CookieContainer();
cookies.Add(new Cookie("B1SESSION", obj.SessionId.ToString()) { Domain = "localhost" });
WebReq.CookieContainer = cookies;
var Cache = new CredentialCache();
Cache.Add(new Uri(url),"Oscar",new NetworkCredential("Desarrollo","S4pb1234"));
WebReq.Credentials = Cache;
WebReq.PreAuthenticate = true;
var httpResponse = (HttpWebResponse)WebReq.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Console.WriteLine(httpResponse.StatusCode);
Console.ReadLine();
}