403禁止Unity3D C# [英] 403 Forbidden Unity3D C#
问题描述
我一直在尝试从以下链接获取信息:隐藏
I've been trying to get the information from this link: HIDDEN
这是我当前的代码
public static string newsLink = "HIDDEN";
public static readonly List<string> newsList = new List<string>();
void Start () {
DontDestroyOnLoad (gameObject);
StartCoroutine (GetNews ());
}
IEnumerator GetNews(){
WWW w = new WWW (newsLink);
yield return w;
if (w.error != null) {
print (w.error);
} else {
List<string>temp = w.text.Split (']').ToList ();
foreach(string a in temp)
{
newsList.Add (a);
}
w.Dispose ();
}
}
}
在调试器中,我收到403 Forbidden的错误消息
in the debugger I get the error message of 403 Forbidden
推荐答案
您要请求的url要求您提供User-Agent来标识您的身份.您可能需要其他标头才能获得适当的响应,但是需要User-Agent标头才能消除服务器抛出的403
错误.
The url you are making request to requires that you provide the User-Agent to identify who you are. You may need other headers to get the appropriate response but the User-Agent header is required to remove that 403
error thrown by the server.
创建用户代理(伪装为Chrome)
Create the User-Agent (pretend to be Chrome)
string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
Dictionary<string, string> ht = new Dictionary<string, string>();
ht["User-Agent"] = userAgent;
然后使用标题创建WWW
请求
WWW w = new WWW(newsLink, null, ht);
yield return w;
请注意,您最终可能会收到HTML和Javascript代码,而不是从Web浏览器访问链接时收到的消息.这是因为您的客户端(Unity)不支持Javascript,并且无法执行Javascript代码.您必须使用php重新编写服务器代码.最后,使用json代替]
或|
分隔您的消息.
Note that you may end up receiving your Html and Javascript code instead of the message you get when you visit the link from a web browser. That's because your client(Unity) does not support Javascript and cannot execute your Javascript code.You have to re-write the server code with php. Finally, use json instead of ]
or |
to seperate your messages.
这篇关于403禁止Unity3D C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!