我正在嘗試通過 C# 登錄網站
string url = "https://drs.zalohovysystem.sk/"
string param = "username=myusername&password=mypass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = param.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
using (Stream stream = request.GetRequestStream())
{
byte[] paramAsBytes = Encoding.Default.GetBytes(param);
stream.Write(paramAsBytes, 0, paramAsBytes.Count());
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //here i get error 405
{
foreach (var cookie in response.Cookies)
{
var properties = cookie.GetType()
.GetProperties()
.Select(p => new
{
Name = p.Name,
Value = p.GetValue(cookie)
});
foreach (var property in properties)
{
Console.WriteLine("{0}: {1}", property.Name, property.Value);
}
}
}
我明白了:
引發例外:System.dll 中的“System.Net.WebException” System.dll 中發生“System.Net.WebException”型別的未處理例外遠程服務器回傳錯誤:(405)方法不允許。
uj5u.com熱心網友回復:
錯誤訊息告訴POST端點上不允許使用該方法。在這種情況下,最常見的問題是:
- 您可能使用了錯誤的端點(這里就是這種情況)。
- 您可能使用了錯誤的 HTTP 方法。
如何找出問題
- 打開 Chrome 或 Edge 等瀏覽器并瀏覽到登錄頁面
- 按F12打開開發者工具
- 選擇網路選項卡,并確保它正在捕獲流量(記錄按鈕應為紅色)
- 輸入憑據并按登錄按鈕。
- 查看請求詳細資訊,包括請求標頭和有效負載選項卡。

如何發送請求并獲得回應
您應該發送:
POST請求https://drs-function-prod.azurewebsites.net/api/v1/Auth/Login端點- 設定
Content-Type: application/json - 發送有效載荷,如
{"login":"[email protected]","password":"xyz"}
然后您將收到以下回復:

例如:
static HttpClient client = new HttpClient();
private async void button1_Click(object sender, EventArgs e)
{
var jsonStr = JsonConvert.SerializeObject(new { login="[email protected]", password ="xyz" });
var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");
var response = await client.PostAsync(
"https://drs-function-prod.azurewebsites.net/api/v1/Auth/Login", content);
MessageBox.Show($"Status: {response.StatusCode}\n"
$"{await response.Content.ReadAsStringAsync()}");
}
uj5u.com熱心網友回復:
確保客戶端密鑰正確,也嘗試設定:
請求.接受=“ / ”;request.UserAgent = "我的應用";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/439888.html
上一篇:查找陣列中的重復項數
下一篇:JWT和身份驗證安全/模式
