Micah Holmes 121 Reputation points

Having a little trouble getting this request to work properly. I'm getting a 503 error. However, I think its something I'm doing wrong in my code. The server works fine when using Postman for test.


using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
namespace _360_API_Testing  
    public class Authentication  
        public string userName { get; set; }  
        public string password { get; set; }  


private bool Authenticate_Connection(string enpoint, string url, object obj)  
            bool Connected = false;  
            Authentication auth = new Authentication();  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(enpoint + url);  
            request.Method = "POST";  
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();  
            Byte[] byteArray = encoding.GetBytes(JsonConvert.SerializeObject(obj));  
            request.ContentLength = byteArray.Length;  
            request.ContentType = @"application/json";  
            using (Stream dataStream = request.GetRequestStream())  
                dataStream.Write(byteArray, 0, byteArray.Length);  
                string auth_token = string.Empty;  
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())  
                    // Got response  
                    auth_token = JsonConvert.DeserializeObject<string>(response.ContentType);  
                //Save Token To Config  
                if (auth_token.Length > 0)  
                    ConfigurationSettings.AppSettings["apiToken"] = "";  
                    ConfigurationSettings.AppSettings["apiToken"] = auth_token;  
            catch (WebException ex)  
                WebResponse errorResponse = ex.Response;  
                using (Stream responseStream = errorResponse.GetResponseStream())  
                    StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));  
                    String errorText = reader.ReadToEnd();  
                    // Log errorText  
                Connected = false;  
            return Connected;  

I'm use to use Unity for webrequests. So the .Net syntax is a little different.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,405 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,156 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,846 Reputation points

    Couple of suggestions. Use a tool like fiddler and call web service from post man to see what the request looks like. Then do the same with your app to see what is different. It should help you figure out what is different and fix your issue. Maybe there is a missing header from your app

    Since you are using httpWebRequest you might need to tell it to use tls1.2. Add this line of code before you call the rest service

       ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

1 additional answer

Sort by: Most helpful
  1. Bruce ( 54,621 Reputation points

    the error typically means the url is bad.