Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 403 Unity3D C中的错误#_C#_Post_Http Request - Fatal编程技术网

C# 403 Unity3D C中的错误#

C# 403 Unity3D C中的错误#,c#,post,http-request,C#,Post,Http Request,我有以下代码: HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create("http://carkit.kg"); tokenRequest.CookieContainer = new CookieContainer(); string token = ""; using (var response = (HttpWebResponse)tokenRequest.GetResponse()) { token = res

我有以下代码:

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应为,在其他情况下,则应为carkit.kg/game

最后一个请求和第一个请求有相同的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错误,请尝试以下操作:

为什么要将1添加到data.length?我会试试看


loginRequest.ContentLength=数据.Length

我尝试了你的代码(问题更新)。但是它没有帮助((请求超时)您的Web服务器是否具有基本或摘要身份验证?听起来您的Web服务器有问题,而不是您的代码是的,我解决了问题:我已将ContentLength设置为数据。Length+1。我删除“+1”并开始工作。谢谢。但现在,我在上面代码的同一位置收到403错误(更新)是的,这是一个身份验证问题,我已经解释过了。检查您的Web服务器如何执行身份验证基本或摘要-进行上述更改。我还尝试了协商和承载。我尝试将域添加到NetworkCredentials构造函数。没有任何帮助