Abdalla Omran

Abdalla Omran

  • NA
  • 334
  • 32.4k

a Problem with POST Request in WebForms ?

Mar 16 2020 7:31 AM
I am trying to read API for example like "https://test......." with POST request and Basic authentication . 
the POST request has Body with URL . i should pass url insaid a body to get json data back which i want to convert it into C# object but the Problem that i am getting always the erro 401 or 405 and i cant get the response . 
 my goal is to get the json data back but how could i pass the url which is in Body ? and why i am getting this erros 401,405 . 
for any help i will be happy 
  1. private string LoadHttpPageWithBasicAuthentication()  
  2.        {  
  3.            //List<WebShopWebTexte> webShopWebTextes = new List<WebShopWebTexte>();  
  4.            Uri myUri = new Uri("https://.......");  
  5.            WebRequest request = HttpWebRequest.Create(myUri);  
  6.            request.Method = WebRequestMethods.Http.Post;  
  7.   
  8.            request.ContentType = "application/json";  
  9.            
  10.            HttpWebRequest httpWebRequest = (HttpWebRequest)request;  
  11.              
  12.            NetworkCredential myNetworkCredential = new NetworkCredential("++++""++++");  
  13.   
  14.            CredentialCache myCredentialCache = new CredentialCache();  
  15.            myCredentialCache.Add(myUri, "Basic", myNetworkCredential);  
  16.   
  17.            httpWebRequest.PreAuthenticate = true;  
  18.            httpWebRequest.Credentials = myCredentialCache;  
  19.   
  20.            WebResponse response = request.GetResponse();  
  21.   
  22.            Stream responseStream = response.GetResponseStream();  
  23.           
  24.            StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);  
  25.   
  26.            string pageContent = myStreamReader.ReadToEnd();  
  27.   
  28.            responseStream.Close();  
  29.   
  30.            response.Close();  
  31.   
  32.            return pageContent;  
  33.   
  34.        }  
 

Answers (1)