Esempio n. 1
0
    public void SendDataBtn()
    {
        List <ObjectData> objectDatas = new List <ObjectData>();

        for (int i = 0; i < 500; i++)
        {
            objectDatas.Add(new ObjectData()
            {
                scopeId   = "test01",
                timeStamp = DateTime.Now,
                type      = "Test",
                x         = Random.Range(-1, 2),
                y         = Random.Range(-1, 2),
            });
        }

        var o = JsonConvert.SerializeObject(objectDatas);
        var h = new HTTPRequest(new Uri("http://127.0.0.1:5000/uploadData"), HTTPMethods.Post, OnRequestFinished)
        {
            RawData = Encoding.UTF8.GetBytes(o)
        };

        if (!h.HasHeader("Content-Type"))
        {
            h.SetHeader("Content-Type", "application/json");
        }

        h.Send();
    }
 public void reSendRequest(HTTPRequest originalRequest, string token)
 {
     if (originalRequest.HasHeader("token"))
     {
         originalRequest.SetHeader("token", token);
     }
     else
     {
         originalRequest.AddHeader("token", token);
     }
     originalRequest.Send();
 }
Esempio n. 3
0
    public void SendDataToServer(MotionType type)
    {
        isFinished = false;
        _dataList.ForEach(p => p.Type = type.ToString());
        var o = JsonConvert.SerializeObject(_dataList);
        // Debug.Log(o);
        var request = new HTTPRequest(new Uri(SystemLogic.Instance.Config.ServerPath + "uploadData"), HTTPMethods.Post, OnRequestFinished)
        {
            RawData = Encoding.UTF8.GetBytes(o)
        };

        if (!request.HasHeader("Content-Type"))
        {
            request.SetHeader("Content-Type", "application/json");
        }
        request.Send();
        _dataList.Clear();
    }
Esempio n. 4
0
        ///connect to wmlab
        ///

        public void SendDataToServer(string finishMinutes, string finishSeconds, int wrongtimes)
        {
            _dataList?.Add(new ATMResultData(finishMinutes, finishSeconds, wrongtimes)
            {
                Tasktype   = finishMinutes,
                Finishtime = finishSeconds,
                Wrongtimes = wrongtimes
            });

            isFinished = false;
            var o = JsonConvert.SerializeObject(_dataList);
            // Debug.Log(o);
            var request = new HTTPRequest(new Uri(LabTools.GetConfig <LabDataConfig>().ServerPath), HTTPMethods.Post, OnRequestFinished) //POST
            {
                RawData = Encoding.UTF8.GetBytes(o)
            };

            if (!request.HasHeader("Content-Type"))
            {
                request.SetHeader("Content-Type", "application/json");
            }
            request.Send();
            Debug.Log("request.Send()");
        }
Esempio n. 5
0
        public static void HandleResponse(string context, HTTPRequest request, out bool resendRequest, out HTTPConnectionStates proposedConnectionState, ref KeepAliveHeader keepAlive)
        {
            resendRequest           = false;
            proposedConnectionState = HTTPConnectionStates.Processing;

            if (request.Response != null)
            {
#if !BESTHTTP_DISABLE_COOKIES
                // Try to store cookies before we do anything else, as we may remove the response deleting the cookies as well.
                if (request.IsCookiesEnabled && CookieJar.Set(request.Response))
                {
                    PluginEventHelper.EnqueuePluginEvent(new PluginEventInfo(PluginEvents.SaveCookieLibrary));
                }
#endif

                switch (request.Response.StatusCode)
                {
                // Not authorized
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
                case 401:
                {
                    string authHeader = DigestStore.FindBest(request.Response.GetHeaderValues("www-authenticate"));
                    if (!string.IsNullOrEmpty(authHeader))
                    {
                        var digest = DigestStore.GetOrCreate(request.CurrentUri);
                        digest.ParseChallange(authHeader);

                        if (request.Credentials != null && digest.IsUriProtected(request.CurrentUri) && (!request.HasHeader("Authorization") || digest.Stale))
                        {
                            resendRequest = true;
                        }
                    }

                    goto default;
                }

                // Redirected
                case 301:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
                case 302:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
                case 307:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
                case 308:     // http://tools.ietf.org/html/rfc7238
                {
                    if (request.RedirectCount >= request.MaxRedirects)
                    {
                        goto default;
                    }
                    request.RedirectCount++;

                    string location = request.Response.GetFirstHeaderValue("location");
                    if (!string.IsNullOrEmpty(location))
                    {
                        Uri redirectUri = ConnectionHelper.GetRedirectUri(request, location);

                        if (HTTPManager.Logger.Level == Logger.Loglevels.All)
                        {
                            HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - Redirected to Location: '{1}' redirectUri: '{1}'", context, location, redirectUri));
                        }

                        // Let the user to take some control over the redirection
                        if (!request.CallOnBeforeRedirection(redirectUri))
                        {
                            HTTPManager.Logger.Information("HTTPConnection", string.Format("[{0}] OnBeforeRedirection returned False", context));
                            goto default;
                        }

                        // Remove the previously set Host header.
                        request.RemoveHeader("Host");

                        // Set the Referer header to the last Uri.
                        request.SetHeader("Referer", request.CurrentUri.ToString());

                        // Set the new Uri, the CurrentUri will return this while the IsRedirected property is true
                        request.RedirectUri = redirectUri;

                        // Discard the redirect response, we don't need it any more
                        request.Response = null;

                        request.IsRedirected = true;

                        resendRequest = true;
                    }
                    else
                    {
                        throw new Exception(string.Format("[{0}] Got redirect status({1}) without 'location' header!", context, request.Response.StatusCode.ToString()));
                    }

                    goto default;
                }

#if !BESTHTTP_DISABLE_CACHING
                case 304:
                    if (request.DisableCache)
                    {
                        break;
                    }

                    if (ConnectionHelper.LoadFromCache(context, request))
                    {
                        HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - HandleResponse - Loaded from cache successfully!", context));
                    }
                    else
                    {
                        HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - HandleResponse - Loaded from cache failed!", context));
                        resendRequest = true;
                    }

                    break;
#endif

                default:
#if !BESTHTTP_DISABLE_CACHING
                    ConnectionHelper.TryStoreInCache(request);
#endif
                    break;
                }

                // Closing the stream is done manually?
                if (!request.Response.IsClosedManually)
                {
                    // If we have a response and the server telling us that it closed the connection after the message sent to us, then
                    //  we will close the connection too.
                    bool closeByServer = request.Response.HasHeaderWithValue("connection", "close");
                    bool closeByClient = !request.IsKeepAlive;

                    if (closeByServer || closeByClient)
                    {
                        proposedConnectionState = HTTPConnectionStates.Closed;
                    }
                    else if (request.Response != null)
                    {
                        var keepAliveheaderValues = request.Response.GetHeaderValues("keep-alive");
                        if (keepAliveheaderValues != null && keepAliveheaderValues.Count > 0)
                        {
                            if (keepAlive == null)
                            {
                                keepAlive = new KeepAliveHeader();
                            }
                            keepAlive.Parse(keepAliveheaderValues);
                        }
                    }
                }
            }
        }