我刚刚在Visual Studio 2015中创建了一个新的C#项目,我运行了该项目,但最终出现错误。
此请求的授权已被拒绝。
但是它给邮递员带来了预期的结果
这是我的代码
var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"https://uatweb.sampath.lk/webservicesRest/api/lankabell/recoverycalling/v1/getrecoveryaccount");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("ServiceName", "GetRecoveryAccountForLankabell");
httpWebRequest.Headers.Add("TockenID", "0");
httpWebRequest.Method = "POST";
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var bodydata = new BodyData { userid = "RANIL", RequestTime = DateTime.Now.ToString()};
string json = JsonConvert.SerializeObject(bodydata);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responce = streamReader.ReadToEnd();
}
}
catch (WebException ex)
{
var pageContent = new StreamReader(ex.Response.GetResponseStream())
.ReadToEnd();
}
}