Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTP 401 Error while posting XML to REST
    primarykey
    data
    text
    <p>I am trying to post a XML to REST Service. Here's the code I am using:</p> <p>I am getting following error, while calling the service.</p> <p><strong>The remote server returned an error: (401) Unauthorized.</strong></p> <p>I have also tried setting NetworkCredentials directly i.e.</p> <pre><code>NetworkCredential nc = new NetworkCredential(username, password); serviceRequest.Credentials = nc; </code></pre> <p>Thanks for your help.</p> <pre><code>Uri address = new Uri("https://localhost:30000/restservice/"); // Create the web request HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.ContentType = "application/json"; string data = @"&lt;Sample XML Here&gt;"; // Create a byte array of the data we want to send byte[] byteData = UTF8Encoding.UTF8.GetBytes(data); // Set the content length in the request headers request.ContentLength = byteData.Length; // Write data using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } string usernamePassword = username + ":" + password; CredentialCache mycache = new CredentialCache(); mycache.Add(address, "Basic", new NetworkCredential(username, password)); request.Credentials = mycache; // Get response using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); // Console application output Response.Write(reader.ReadToEnd()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload