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.