2

I want to call a rest client with basic authorization. I tried this but got a Unauthorized(401) exception in c# :

HttpWebRequest client = (HttpWebRequest)WebRequest.Create("http://someurl.com");
            client.Method = "POST";
            client.UseDefaultCredentials = true;
            var encoding = new UTF8Encoding();
            var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(mailinglist.ToString());
            client.ContentLength = bytes.Length;
            using (var writeStream = client.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }
            client.ContentType = "application/json";
            NetworkCredential nc = new NetworkCredential("8ec5f23e-18ba-4154-9962-7ebefeb027c0", "");
            //string credentials = String.Format("{0}:{1}", "8ec5f23e-18ba-4154-9962-7ebefeb027c0", "");
            //byte[] bts = Encoding.ASCII.GetBytes(credentials);
            //string base64 = Convert.ToBase64String(bts);
            client.PreAuthenticate = true;
            //string authorization = String.Concat("Basic ", base64);
            //Utils.WriteLog("Addaudience", authorization);
            //client.Headers.Add(HttpRequestHeader.Authorization, authorization);
            client.Credentials = nc;
            client.Accept = "application/json";

            using (var response = (HttpWebResponse)client.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    
                    throw new ApplicationException(message);
                }

                // grab the response
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }
            }

Please help me and tell me what is my mistake? I also tried to put Authorization string in the headers but not working.

Thanks.

fakhrad
  • 21
  • 2
  • I tried to call the Api with postman and it works!! – fakhrad Oct 12 '17 at 11:31
  • How are you calling it via postman? How are you sending the credentials via postman? – Chetan Oct 12 '17 at 12:28
  • I used basic authentication one time and it works. next I put Authorization at headers it also worked – fakhrad Oct 12 '17 at 12:43
  • Authorization: Basic OGVjNWYyM2UtMThiYS00MTU0LTk5NjItN2ViZWZlYjAyN2MwOg== Accept: application/json Content-Type: application/json Cache-Control: no-cache Postman-Token:987987-897987-kjkhkhj-9879 { "Name": "name", "FromName": "test name", "FromEmail": "admin@test.com", "Description": "Sample list", "CharacterSet": "utf-8", "ReplyToEmail": "reply@test.com" } – fakhrad Oct 12 '17 at 12:44
  • You need to add headers Authorization and Content-Type in the request when you make API call from code.https://stackoverflow.com/questions/8519788/add-custom-header-in-httpwebrequest – Chetan Oct 12 '17 at 13:59
  • Yes I added Authorization and Content-type to request headers but still not working :( – fakhrad Oct 12 '17 at 14:18

0 Answers0