我正在使用C#向我的网络服务器发出请求,这样我就可以将客户端的数据保存到我的模型中。
这是路线:
Route::get('client/register', 'ClientController@store');
C#请求:
string response = Network.sendWebRequest("http://localhost/myproject/public/client/register?identity=" + getIdentity());
如您所见,我发送了一个包含identity
和base64
编码字符串作为值的GET请求。
然后在我的商店函数中我解码base64编码的字符串,将其爆炸并尝试将其保存在我的数据库中(提示:数据库表为空,所以不要担心该记录不存在已)
ClientController
public function store(Request $request)
{
echo "store function";
$clientIdentityB64 = request('identity');
if (empty($clientIdentityB64)) { return "empty"; }
$clientIdentity = base64_decode($clientIdentityB64);
$identityArr = explode(";", $clientIdentity);
$clientQuery = Client::select("id")
->where("base64Id", "=", $clientIdentityB64)
->get()
->first();
if (is_null($clientQuery)) {
$client = new Client();
$client->ip = $request->ip();
$client->base64Id = $clientIdentityB64;
$client->userName = $identityArr[0];
$client->userDomainName = $identityArr[1];
$client->machineName = $identityArr[2];
$client->osVersion = $identityArr[3];
$client->userAgent = $request->userAgent();
$client->save();
}
}
但是,如果我发出请求,它不会将模型保存到数据库中。您可以看到我在store函数的顶部输出store function
,但是如果我从代码中删除$client->save()
,我只会得到响应。
如果代码中有$client->save()
,那么我会收到此回复:
Exception: System.Net.WebException: Request canceled: disconnected
bei System.Net.HttpWebRequest.GetResponse()
bei Helper.Network.sendWebRequest(String URL) in C:\Users....
我已经成功地回显了所有值,只有保存功能不起作用,即使值存在。
但是,它确实有效如果我从普通的Web浏览器调用以下URL:http://localhost/myproject/public/client/register?identity=somebase64encodedhash
。
如何解决这个问题?
这是我上面使用的网络课程:
public static class Network
{
public static string sendWebRequest(string URL)
{
System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
try {
System.Net.WebResponse response = request.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
} catch (Exception e) {
return "Exception: " + e;
}
}
}
来自Webbrowser的请求:
GET /myproject/public/client/register?identity=LO0dzILO0E5ULO0IBXaW5kb3dzIEDUNLO0JMQUNLO01pY3Jvc29mdCBXaW5kb3dzIE5UIDYuMi45MjAwLjA= HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: de,en-US;q=0.7,en;q=0.3 Client-Ip: 147.153.195.175 Connection: keep-alive Cookie: XSRF-TOKEN=eyJpdiI6IkhPNW9yeVhkNGFiQWFnWUtFQ2FRREE9PSIsInZhbHVlIjoiZm1yR1NxN0tvTEFJSTNoYnJWdmNRWWZEVTRDOWVYUHJNemhDWCs4TFY4VnNoU2J5ZXNha3dPNHAwNHlKc0lHMlRGQUdiUE9BdnJcL2NDb1l4RytYSFJRPT0iLCJtYWMiOiI2ODliODM1ZDU4NzFlMTcxNmUyNGMzYjNhYTBkNGU0NjI3NDQ4MmYxZTFiZTg5YWNjMmEyZWFiMDliYWExZjY4In0%3D; laravel_session=eyJpdiI6Ijl0Mnlid0xkdTRRQVh2UDZhXC95MzFBPT0iLCJ2YWx1ZSI6InR4eCtTRVg5XC81YTU3dHB1azluVHpYemw5a2ZkbnBEMGdsRllGSXFIajFJSUdWcitwSTFQS1pWXC9LUlR3Vzh0eExSY3Via2xwOTBhVG5kZXVzRXdWbGc9PSIsIm1hYyI6Ijg5MmZiNDMxOTFhMGQzNjMxNmM0NzI1NzAyOTk3ZTU4ODkwMzdkYzM3YTIxZTU3M2MzMWYzYjM5NTI1OWVhZDQifQ%3D%3D Dnt: 1 Host: localhost Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0 Via: 223.100.9.3 X-Forwarded-For: 36.2.247.179
来自C#应用程序的请求:
GET /myproject/public/client/register?identity=LO0dzILO0E5ULO0IBXaW5kb3dzIEDUNLO0JMQUNLO01pY3Jvc29mdCBXaW5kb3dzIE5UIDYuMi45MjAwLjA= HTTP/1.1
Host: localhost
提示:我将base64字符串更改为其他内容...
我将我的网络类更改为此,因此我可以将空cookieContainer附加到请求中。但它根本不会改变请求。
public static class Network
{
public static string sendWebRequest(string URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.CookieContainer = new CookieContainer();
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
} catch (Exception e) {
return "Exception: " + e;
}
}
}
答案 0 :(得分:0)
我通过在我的C#表单中添加一个webBrowser元素并从浏览器中调用URL来解决它:
{%- .. -%}