我正在尝试使用HttpWebRequest(POST)将一些数据发送到服务器。但是我总是得到错误:(403)禁止。这是我目前的代码:
foreach (Patient patient in patients)
{
string[] names = patient.Nome.Split(' ');
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ehr/rest/createPerson");
request.Headers.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXh0cmFkYXRhIjp7Im9yZ2FuaXphdGlvbiI6IjY2NjYifSwiaXNzdWVkX2F0IjoiMjAxNi0wNC0yMlQxMzo0ODoyOS40ODRaIn0=.Fztrd8LR/FkBY2CJLjnl/qYYYjz1/vr4g01e8yTad/0=");
request.Accept = "application/json";
string tempUrl = "firstName=" + names[0];
tempUrl += "&lastName=" + names[names.Length - 1];
tempUrl += "&dob=" + patient.Data_Nasc;
tempUrl += "&role=pat";
tempUrl += "&sex=" + "M";
tempUrl += "&organizationUid=" + organization.Uid;
var data = Encoding.ASCII.GetBytes(tempUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
我知道有很多关于此错误的问题,但没有答案可以解决我的问题。我仍然得到错误403。 接下来是stacktrace:
at System.Net.HttpWebRequest.GetResponse()
at IndexDocClinicos.Classes.EhrData.createPersons() in c:\Users\Joaogcorreia\Desktop\EHR + Solr + IndexDocClinicos\Indexacao-de-Documentos-Clinicos\Indexação de Documentos Clinicos\IndexDocClinicos\Classes\EhrData.cs:line 163
at IndexDocClinicos.WebApiApplication.Application_Start() in c:\Users\Joaogcorreia\Desktop\EHR + Solr + IndexDocClinicos\Indexacao-de-Documentos-Clinicos\Indexação de Documentos Clinicos\IndexDocClinicos\Global.asax.cs:line 37
我已经用Insomnia尝试了这个POST,效果很好。所以我没有想到什么是错的。我也验证了,令牌是正确的。
请帮助
thanks =)