[英]How can I post something like this?
I have a problem with the API in a console.我在控制台中的 API 有问题。 So I want to post and I always get 411 error or 403. This is my code:所以我想发帖,我总是收到 411 错误或 403。这是我的代码:
string IntId = "suli";
var lekeres = WebRequest.Create("https://xxxx.e-kreta.hu/idp/api/v1/Token") as HttpWebRequest;
lekeres.Method = "POST";
string adatokkal = "institute_code=" + IntId + "&userName=" + azonosito + "&password=" + jelszo + "&grant_type=password&client_id=919e0c1c-76a2-4646-a2fb-7085bbbf3c56";
lekeres.Headers.Add(HttpRequestHeader.Authorization,adatokkal);
var response = lekeres.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
The origin Curl command (It works):原点卷曲命令(它有效):
curl --data "institute_code=xxxxxxxxx&userName=xxxxxxxxxxx&password=xxxxxxxxxxx&grant_type=password&client_id=919e0c1c-76a2-4646-a2fb-7085bbbf3c56" https://xxxxxxxxxxx.e-kreta.hu/idp/api/v1/Token
Thanks for your help!谢谢你的帮助!
You could always just use a Webclient:你总是可以只使用一个 Webclient:
using (WebClient client = new WebClient())
{
string adatokkal = "https://xxxx.e-kreta.hu/idp/api/v1/Token?institute_code=" + IntId + "&userName=" + azonosito + "&password=" + jelszo + "&grant_type=password&client_id=919e0c1c-76a2-4646-a2fb-7085bbbf3c56";
string Response = client.DownloadString(new Uri(AH_Data_Url));
}
Or a HTTP client或者一个 HTTP 客户端
var client = new HttpClient { BaseAddress = new Uri("https://xxxx.e-kreta.hu") }; var request = new HttpRequestMessage(HttpMethod.Post, "/idp/api/v1/Token"); var formData = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("institute_code", IntId ) new KeyValuePair<string, string>("userName", azonosito ) // Add the rest here }; request.Content = new FormUrlEncodedContent(formData); var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode == true) { var responseContent = response.Content; string responsestring = responseContent.ReadAsStringAsync().Result; } else { }
If you have to authorize your request you should add something like如果您必须授权您的请求,您应该添加类似
var byteArray = new UTF8Encoding().GetBytes("Client ID" + ":" + "Client Secret");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Since your curl -d
(just a simple POST) works, you need to write your data to the request body, not the Authorization
header as you have it.由于您的curl -d
(只是一个简单的 POST)工作,您需要将您的数据写入请求正文,而不是您拥有的Authorization
标头。 I think this should do it:我认为应该这样做:
string IntId = "suli";
var lekeres = WebRequest.Create("https://xxxx.e-kreta.hu/idp/api/v1/Token") as HttpWebRequest;
lekeres.Method = "POST";
string adatokkal = "institute_code=" + IntId + "&userName=" + azonosito + "&password=" + jelszo + "&grant_type=password&client_id=919e0c1c-76a2-4646-a2fb-7085bbbf3c56";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
lekeres.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
lekeres.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = lekeres.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
var response = lekeres.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.