我如何发布这样的内容?

时间:2019-04-12 18:10:55

标签: c# api post

我在控制台中的API有问题。所以我想发布,我总是收到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);
            }

起源Curl命令(有效):

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您始终可以只使用Web客户端:

                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));
                }

或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
            {

            }

如果必须授权您的请求,则应添加类似内容

        var byteArray = new UTF8Encoding().GetBytes("Client ID" + ":" + "Client Secret");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

答案 1 :(得分:0)

由于您的curl -d(只是一个简单的POST)起作用,因此您需要将数据写入请求正文,而不是像现有的Authorization标头那样。我认为应该这样做:

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);
}