-1

I am seting up simple console application in C# for some API integration. I need to consume API with POST Request with XML data as request body and with Basic Authentication.

I am able to consume API for retrieval with Basic auth, but I am not sure how to consume API with post which having basic auth.

1

2 Answers 2

0

Refer to this answer it shows the full android app including xml and java file link https://stackoverflow.com/a/57162854/11505004

Sign up to request clarification or add additional context in comments.

Comments

0

Below is the way I did. Using BASIC Authentication and XML data as the payload. Happy Coding :)

    SomeClass{
            private NetworkCredential credentials = null;
            private HttpClientHandler handler = null;                
            SomeClass(){ //Constructor
                  credentials = new NetworkCredential("ID", "PASS");
                  handler = new HttpClientHandler { Credentials = credentials};
                }  
        
     public async Task<HttpResponseMessage> PutMessage(your data) 
        {           
            var root= new
            {
                rootObj = data
            };    
            var jsonObj = JsonConvert.SerializeObject(root);
            var doc = JsonConvert.DeserializeXmlNode(jsonObj); 
            HttpResponseMessage response = null;            
            try
            {   
                var httpContent = new StringContent(doc.InnerXml.ToString(), Encoding.UTF8, "application/xml");

                HttpClient httpClient = new HttpClient(handler);
                response = await httpClient.PostAsync(APIURL, httpContent);
                if (!response.IsSuccessStatusCode)                      
                    return response;
            }
            catch (Exception ex)
            {               
                return "Return Error Message";
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.