Пример #1
0
 public MegaUpload(Uri uri, int lengthUpload)
 {
     request = new HttpRequest_(uri, "POST");
     request.AddHeader("HOST", uri.Host);
     request.AddHeader("Content-Type", "application/octet-stream");
     request.AddHeader("Content-Length", lengthUpload.ToString());
 }
Пример #2
0
        private string GetAccessToken_(string key_authorize, int port)
        {
            Uri uri = new Uri("https://api.dropboxapi.com/1/oauth2/token" +
                              string.Format("?code={0}&client_id={1}&client_secret={2}&grant_type={3}&redirect_uri=http%3A%2F%2Flocalhost%3A{4}"
                                            , key_authorize, DropboxAppKey.ApiKey, DropboxAppKey.ApiSecret, "authorization_code", port.ToString()));

            HttpRequest_ rq = new HttpRequest_(uri, TypeRequest.POST.ToString());

            rq.AddHeader("HOST: api.dropboxapi.com");
            rq.AddHeader("Content-Length: 0");
            return(rq.GetTextDataResponse(true, true));
        }
Пример #3
0
        private RequestReturn Request <T>(string url, TypeRequest typerequest, byte[] bytedata = null, string[] moreheader = null)
        {
            RequestReturn result = new RequestReturn();

            http_request = new HttpRequest_(new Uri(url), typerequest.ToString());
#if DEBUG
            http_request.debug = Debug;
#endif
            if (typeof(T) == typeof(Stream) && bytedata == null && typerequest != TypeRequest.PUT)
            {
                http_request.ReceiveTimeout = this.ReceiveTimeout_;
            }
            http_request.AddHeader(Host);
            http_request.AddHeader("Authorization", "Bearer " + Token.access_token);
            if (moreheader != null)
            {
                foreach (string h in moreheader)
                {
                    http_request.AddHeader(h);
                }
            }
            else if (bytedata != null && (typerequest == TypeRequest.POST || typerequest == TypeRequest.PATCH))
            {
                http_request.AddHeader(Header_ContentTypeApplicationJson);
            }
            if ((typerequest == TypeRequest.POST || typerequest == TypeRequest.DELETE) && bytedata == null)
            {
                http_request.AddHeader("Content-Length: 0");
            }
            if (bytedata != null && (typerequest == TypeRequest.POST || typerequest == TypeRequest.PATCH))
            {
                http_request.AddHeader("Content-Length: " + bytedata.Length.ToString());
                Stream stream = http_request.SendHeader_And_GetStream();
                stream.Write(bytedata, 0, bytedata.Length);
                stream.Flush();
#if DEBUG
                Console.WriteLine("DriveAPIHttprequestv2: >>send data: " + Encoding.UTF8.GetString(bytedata));
#endif
            }
            try //get response
            {
                if (typeof(T) == typeof(string))
                {
                    result.DataTextResponse = http_request.GetTextDataResponse(false, true);
                    result.HeaderResponse   = http_request.HeaderReceive;
                }
                else if (typeof(T) == typeof(Stream))
                {
                    if (bytedata != null || typerequest == TypeRequest.PUT)
                    {
                        result.stream = http_request.SendHeader_And_GetStream();                                                    //get stream upload
                    }
                    else
                    {
                        result.stream = http_request.ReadHeaderResponse_and_GetStreamResponse(true); //get stream response
                    }
                }
                else
                {
                    throw new Exception("Error typereturn.");
                }
                return(result);
            }
            catch (HttpException ex)
            {
                result.DataTextResponse = http_request.TextDataResponse;
                GoogleDriveErrorMessage message;
                try { message = Newtonsoft.Json.JsonConvert.DeserializeObject <GoogleDriveErrorMessage>(ex.Message); }
                catch { throw ex; }// other message;
                switch (message.error.code)
                {
                case 204: if (typerequest == TypeRequest.DELETE)
                    {
                        return(result);
                    }
                    break;                                                                // delete result

                case 401:
                    if (Monitor.TryEnter(SyncRefreshToken))
                    {
                        try
                        {
                            Monitor.Enter(SyncRefreshToken);
                            Token = oauth.RefreshToken();
                            TokenRenewEvent.Invoke(Token);
                            return(Request <T>(url, typerequest, bytedata, moreheader));
                        }
                        finally { Monitor.Exit(SyncRefreshToken); }
                    }
                    else
                    {
                        try { Monitor.Enter(SyncRefreshToken); return(Request <T>(url, typerequest, bytedata, moreheader)); }
                        finally { Monitor.Exit(SyncRefreshToken); }
                    }

                case 403:
                    Error403 err = (Error403)Enum.Parse(typeof(Error403), message.error.errors[0].reason);
                    switch (err)
                    {
                    case Error403.forbidden:
                    case Error403.appNotAuthorizedToFile:
                    case Error403.domainPolicy:
                    case Error403.insufficientFilePermissions:
#if DEBUG
                        Console.WriteLine("DriveAPIHttprequestv2 Error403: " + result.DataTextResponse);
#endif
                        break;

                    case Error403.dailyLimitExceeded:
                    case Error403.rateLimitExceeded:
                    case Error403.sharingRateLimitExceeded:
                    case Error403.userRateLimitExceeded:
                        if (limit != null)
                        {
                            limit.Invoke();
                        }
#if DEBUG
                        Console.WriteLine("DriveAPIHttprequestv2 LimitExceeded: " + err.ToString());
#endif
                        try { Monitor.Enter(SyncLimitExceeded); Thread.Sleep(5000); } finally { Monitor.Exit(SyncLimitExceeded); }
                        return(Request <T>(url, typerequest, bytedata, moreheader));

                    case Error403.abuse:        //file malware or virut
                        if (acknowledgeAbuse_)
                        {
                            return(Request <T>(url + "&acknowledgeAbuse=true", typerequest, bytedata, moreheader));
                        }
                        else
                        {
                            break;
                        }

                    default: break;
                    }
                    break;

                case 308:
                    if (typerequest == TypeRequest.PUT && typeof(T) == typeof(Stream))
                    {
                        result.stream = http_request.GetStream();
                        return(result);
                    }
                    else
                    {
                        break;
                    }

                default: break;
                }
                throw ex;
            }
        }
Пример #4
0
        private T POST_Request <T>(string url, object obj_request = null, string[] headers = null, string ContentType = "application/json", bool upload = false)
        {
            Stream st = null;

            rq = new HttpRequest_(new Uri(url), "POST");
            rq.AddHeader("HOST", "api.dropboxapi.com");
            rq.AddHeader("Authorization", "Bearer " + access_token);

            if (headers != null)
            {
                foreach (string header in headers)
                {
                    rq.AddHeader(header);
                }
            }
            if (obj_request != null && !string.IsNullOrEmpty(ContentType))
            {
                byte[] reqData;
                if (obj_request is byte[])
                {
                    reqData = (byte[])obj_request;
                }
                else
                {
                    reqData = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(obj_request));
                }

                rq.AddHeader("Content-Type", ContentType);
                rq.AddHeader("Content-Length", reqData.Length.ToString());
                st = rq.SendHeader_And_GetStream();
                st.Write(reqData, 0, reqData.Length);
                st.Flush();
            }
            else
            {
                rq.AddHeader("Content-Length", "0");
                st = rq.SendHeader_And_GetStream();
            }


            //receiver
            if (typeof(T) == typeof(Stream))
            {
                if (!upload)
                {
                    st = rq.ReadHeaderResponse_and_GetStreamResponse(true); //download
                }
                return((T)(object)st);                                      //upload
            }
            else
            {
                rq.ReadHeaderResponse_and_GetStreamResponse(true);
                string text_response = rq.ReadDataResponseText();//.Replace("\".tag\":", "\"tag\":");
                if (typeof(T) == typeof(string))
                {
                    return((T)(object)text_response);
                }
                else
                {
                    T data = JsonConvert.DeserializeObject <T>(text_response);
                    if (data != null)
                    {
                        return(data);
                    }
                    else
                    {
                        if (typeof(T) == typeof(Dropbox_Response_Error))
                        {
                            throw new DropboxJsonResponseException(data as IDropbox_Response_Error);
                        }
                        else
                        {
                            throw new Exception(text_response);
                        }
                    }
                }
            }
        }