Last post Mar 16, 2017 01:00 PM by Khuram.Shahzad
Mar 16, 2017 11:57 AM|Sarvesh Kumar Verma|LINK
I am creating a httpwebrequest that requires user id , password to get authenticated. DealerID is also used to get authorized. When I pass user id , password and dealerid correct then everything works fine but when I pass correct Userid password but incorrect
DealerID then c# code returns "The remote server returned an error: (401) Unauthorized." But in fiddler and postman returns "Dealer
xxxxxxxxxx is not subscribed to adp3pa.pip.customer-vehicle-search-extract.standard.endpoint".
Here is code:-
public static string SendRestRequest(string RestServiceUrl,string PostData)
string responseValue = string.Empty;
WebRequest req = WebRequest.Create(@RestServiceUrl);
req.Method = "POST";
req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(Global.CDKUserName + ":" + Global.CDKPassword));
ASCIIEncoding encoding = new ASCIIEncoding();
byte data = encoding.GetBytes(PostData);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
using (Stream putStream = req.GetRequestStream())
putStream.Write(data, 0, data.Length);
HttpWebResponse response = req.GetResponse() as HttpWebResponse;
using (var responseStream = response.GetResponseStream())
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
responseValue = reader.ReadToEnd();
I checked stack trace and Inner Exception but didn't find message "Dealer xxxxxxxxxx is not subscribed to adp3pa.pip.customer-vehicle-search-extract.standard.endpoint".
I have to show appropriate message from c# just like postman and fiddler returns.
Thanks in Advance.
Mar 16, 2017 01:00 PM|Khuram.Shahzad|LINK
It is up to you to have a middleware that can catch the 401 request and then perform operation what you want to do, In case of SPA like Angular JS you can have http interceptor which catch request and if it has 401 status then you can perform any operation.
Its up to application type, in .Net Core you can have middleware, In Angular JS you can have http interceptor, in Asp.Net MVC you can have ajax request and inside ajax request you can catch error.