c# - 将 python 服务器请求转换为统一

标签 c# python web unity3d

我一直在思考如何将 python 中的 Web 身份验证请求统一起来,有人可以帮我解决这个问题吗。

此处“auth”关键字的含义,以及我如何形成相同的形式 unity C#。 我是统一网络概念的新手,请帮助我。

This is my python code:

r = requests.post("my url",
auth = (client_id,client_secret),
data = { 'grant_type' : 'password',
'username' : username, 
'password' : password })

if r. status_code == 200: { my code ...}

In unity i dont know how to write this, i read through WWW,WWWForm classes and used headers also like shown below but always getting 404 error. page not displayed.

This is my unity code :

publicvoidRequestUserLogin(stringpUsername, stringpPassword)
{

Dictionary<string,string> authHeader = newDictionary<string,string> ();
authHeader.Add (mS_Client_Id, mS_Client_Secret);

WWWForm form = newWWWForm();
WWW WWWLogin;

form.AddField ("grant_type", "password");
form.AddField ("username", "dumandu1");
form.AddField ("password", "pwd");
Hashtableheaders = form.headers;
headers ["auth"] = System.Convert.ToBase64String
(System.Text.Encoding.ASCII.GetBytes(mS_Client_Id+":"+mS_Client_Secret));

stringurl = "my url";

WWWLogin = newWWW(url,form);
StartCoroutine(Example(WWWLogin));

}

IEnumeratorExample(WWWWWWLogin) 
{

yield return WWWLogin;

if (WWWLogin.error == null)
{
  Debug.Log("WWW Login Success: "+ WWWLogin.error);
  if (WWWLogin.text != null) 
  {
    Debug.Log("WWW Login Text: "+ WWWLogin.text);
  }
} 
else
{
  Debug.Log("WWW Login Error: "+ WWWLogin.error);
} 

}

为此,我总是收到 404 页面未显示错误。代码在 python 中运行良好,并从服务器获得正确的响应。

我是不是做错了什么。

any help would be great .
thank you.

最佳答案

始终确保在调用任何函数时使用所有必要的数据。

在这种情况下,您将忽略 header 。 new WWW(url, form) 将创建一个没有 header 的请求。你想要的是:

WWWLogin = new WWW(url, form, headers);

希望对您有所帮助。

关于c# - 将 python 服务器请求转换为统一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29003099/

相关文章:

c# - sqlite错误WP8.1

c# - WinSCP .NET 程序集拒绝 RSA/DSA key 指纹

c# - 如何使用 StreamWriter 在 C# 中编辑文本文件?

python - 安装 Python 包 : setup. py install - 错误:未提供命令

javascript - 我的 jQuery 'KeyCode' 代码不工作

c# - AZMan:InitializeClientContextFromName 失败,返回 "Value does not fall within the expected range."

python - 为什么我会得到 this cannot index with vector containing NA/NaN values 错误

python - 在多种情况下返回的 Pandas 语法错误

python - 模板 django-simple-captcha?图像和输入字段之间的换行符

javascript - HTML + CSS 渲染问题