我正在尝试使用Unity创建一个简单的HTTP:GET请求到本地NodeJs服务器。
我的网络受代理保护,但在这种情况下,我正在请求localhost。 顺便说一句,如果我在浏览器或邮递员上测试它,请求就会起作用。
我相信它可能与标题有关。来自UnityWebRequest.SetRequestHeader文档:
无法在任何平台上使用自定义值设置这些标头:accept-charset,access-control-request-headers,access-control-request-method,connection-date,dnt,expect,host,keep-alive,origin ,referer,te,trailer,transfer-encoding,upgrade,via。
我不明白为什么它会袭击代理?
以下是我使用的脚本和响应。
const express = require('express');
const app = express();
app.get('/', function(req, res) {
console.log('test');
return res.sendStatus(200);
})
app.listen(8080, () => {
console.log(`Magic happens on port 8080`);
});
module.exports = app;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Request : MonoBehaviour
{
private GUIStyle guiStyle = new GUIStyle ();
string uri = "http://127.0.0.1:8080";
void Start ()
{
Get();
}
void Update ()
{
}
public void Get ()
{
StartCoroutine (SendGet ());
}
IEnumerator SendGet ()
{
Debug.Log (uri);
using (UnityWebRequest www = UnityWebRequest.Get (uri)) {
yield return www.Send ();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log (www.downloadHandler.text);
byte[] results = www.downloadHandler.data;
Debug.Log (results);
}
}
}
}
<!DOCTYPE html>
<!--
Message.TemplateName: authenticationrequired
Message.Language: Fallback
-->
<html>
<!-- head -->
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>McAfee Web Gateway - Notification - Authentication Required</title>
<script src="/mwg-internal/de5fs23hu73ds/files/javascript/sw.js" type="text/javascript" ></script>
<link rel="stylesheet" type="text/css" href="/mwg-internal/de5fs23hu73ds/files/system/stylesheet.css" />
</head>
<!-- /head -->
<!-- body -->
<body onload="swOnLoad();" style="background-image: url('/mwg-internal/de5fs23hu73ds/files/system/img/bg_body.jpg');">
<!-- wrapper -->
<div class="wrapper">
<!-- maincontent -->
<div class="maincontent" style="background-image: url('/mwg-internal/de5fs23hu73ds/files/system/img/bg_container.png');">
<div class="header" id="header" style="background-image: url('/mwg-internal/de5fs23hu73ds/files/system/img/bg_navbar.png');">
Authentication Required
</div>
<!-- content -->
<div class="content">
You must be authenticated to access this URL.<br/>
<div class="info">
<!-- Client IP -->
<b>Client IP:</b> 10.0.20.188
<br/>
<!-- URL -->
<b>URL:</b> <script type="text/javascript">break_line("http://localhost:8080");</script>
<br/>
</div>
</div>
<!-- /content -->
<!-- action -->
<!-- spacer --><div style="height: 10px;"></div><!-- /spacer -->
<div class="action">
<form name="ftpform" method="get" action="" id="ftpform" style="display:none;">
<table>
<tr><td><b>FTP User Name:</b></td><td><input type="text" name="ftpUsername" id="ftpUsername" /></td></tr>
<tr><td><b>FTP Password:</b></td><td><input type="password" name="ftpPassword" id="ftpPassword" /></td></tr>
<tr><td><b>Please confirm:</b></td><td><input type="button" value="Access FTP" onClick="redirectToFTP();" /></td></tr>
</table>
</form>
</div>
<script language="javascript" type="text/javascript">
urlprotocol = "http";
statuscode=407;
if(statuscode==401 && urlprotocol == "ftp"){
document.getElementById("ftpform").style.display = "block";
}
function redirectToFTP(){
var username=escape(document.getElementById("ftpUsername").value);
var password=escape(document.getElementById("ftpPassword").value);
location.href = "ftp://"+username+":"+password+"@localhost:8080"
}
</script>
<!-- /action -->
<!-- footer -->
<div class="footer">
generated 2018-03-20 08:28:01
by MWGBP01 <span class="notdisplayfalse">(10.0.1.186:8080)</span><br/>
Autenticação: Active Directory(Autenticação: Proxy explícito)
</div>
<!-- /footer -->
</div>
<!-- /maincontent -->
</div>
<!-- /wrapper -->
</body>
<!-- /body -->
</html>
UnityEngine.Debug:Log(Object)
<SendGet>c__Iterator0:MoveNext() (at Assets/Scripts/Request.cs:39)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)