httpWebRequest

Micah Holmes 121 Reputation points
2022-02-03T20:47:54.157+00:00

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.

Object:

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; }  
    }  
}  

Request:

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);  
            }  
  
            try  
            {  
                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,689 questions
C#
C#
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.
11,128 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,856 Reputation points
    2022-02-04T11:44:56.053+00:00

    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

    https://www.telerik.com/fiddler

    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 (SqlWork.com) 68,311 Reputation points
    2022-02-03T20:53:16.807+00:00

    the error typically means the url is bad.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.