简体   繁体   English

我怎么能发布这样的东西?

[英]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.

相关问题 如何实现类似PostSharp的OnFieldAccessAspect? - How can I implement something like OnFieldAccessAspect of PostSharp? 我怎样才能制作一个不和谐的机器人来做这样的事情? - How can I make a discord bot that does something like this? 我怎么能做像IList这样的事情 <T> 。载有(OtherObjectType)? - How can i do something like IList<T>.Contains(OtherObjectType)? 如何使用 FolderBrowserDialog 或类似的东西提示用户输入文件夹? - How can I prompt the user for a folder using a FolderBrowserDialog or something like it? 如何在C#中创建类似动态枚举的东西? - How can I make something like a dynamic enumeration in C#? 我可以在 c# 中做类似 foreachwhile 循环的事情吗(这就是我将如何引用它)? - can I do something like a foreachwhile loop(that's how I will be refering to it) in c#? 我希望在用户在文本框中键入某些内容之前,发送按钮不起作用。 我怎样才能做到这一点? - I would like the send button to be inactive until the user key in something in the textbox. How can I do this? 我如何实现“如果玩家同时按下ctrl和w”这样的动作,移动速度(前向力)将为2000,而不是1000 - How can I implement something like “If the player presses both ctrl and w” the moving speed (forwardforce) will be 2000 instead of like 1000 我可以做这样的事情来按类型名称获取实体吗? - Can I do something like this for get Entity by type name? DevExpress XtraForm 中是否有我可以设计类似collapsePanel 的控件或面板? - Is there a Control or Panel in DevExpress XtraForm that I can design something like a collapsePanel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM