Unity3D c#中的403错误
本文关键字:错误 中的 Unity3D | 更新日期: 2023-09-27 18:15:09
我有以下代码:
HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");
tokenRequest.CookieContainer = new CookieContainer();
string token = "";
using (var response = (HttpWebResponse)tokenRequest.GetResponse()) {
token = response.Cookies["csrftoken"].ToString().Split('=')[1];
}
HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg");
var cache = new CredentialCache();
cache.Add(new Uri("http://carkit.kg/"), "Basic", new NetworkCredential(tempEmail, tempPass));
loginRequest.Credentials = cache;
loginRequest.PreAuthenticate = true;
loginRequest.Method = "POST";
loginRequest.CookieContainer = new CookieContainer();
loginRequest.CookieContainer.Add(new Cookie("csrftoken", token) {Domain="carkit.kg"});
Debug.Log(token);
byte[] data = Encoding.UTF8.GetBytes("username=" + tempEmail + "&password=" + tempPass + "&csrfmiddlewaretoken=" + token);
//loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.ContentLength = data.Length;
loginRequest.Timeout = 3000;
loginRequest.GetRequestStream().Write(data, 0, data.Length);
Debug.LogWarning(loginRequest.Headers.ToString());
HttpWebResponse authResponse = (HttpWebResponse)loginRequest.GetResponse();
Debug.Log(authResponse.ResponseUri);
authResponse。如果密码不正确,ResponseUri应为http://carkit.kg。
最后一个请求有相同的url作为第一个,但我得到403错误的第二个。
在python上有一个代码,我希望c#代码能够做的工作:
import urllib2
main_page_request = requests.get('http://carkit.kg/')
csrf_cookie = main_page_request.cookies.get('csrftoken', '')
r = requests.post('http://carkit.kg/', data={u'username': u'admin', u'password': u'admin', 'csrfmiddlewaretoken': csrf_cookie }, cookies={'csrftoken': csrf_cookie})
print r.url
我猜您没有以正确的格式输入凭证信息。我将使用CredentialCache并设置PreAuthenticate=true下面是相应的代码:
var cache = new CredentialCache();
cache.Add(new Uri(uri), "Digest", new NetworkCredential(username, password));
httpRequest.Credentials = cache;
httpRequest.PreAuthenticate = true;
修复411错误,试试这个:为什么我得到411长度要求错误?
为什么要给data.length加1 ?我想试试
loginRequest。