public JsonResult CheckDBConnection(string dbServer, string dbName, string dbPort, string dbUserName, string dbPassword) { if( string.IsNullOrEmpty(dbServer) ) throw new ArgumentException("服务器地址不能为空."); if( string.IsNullOrEmpty(dbName) ) throw new ArgumentException("数据库名称不能为空."); if( string.IsNullOrEmpty(dbUserName) ) throw new ArgumentException("数据库账号不能为空."); if( string.IsNullOrEmpty(dbPassword) ) throw new ArgumentException("数据库密码不能为空."); string sqlConnection = string.Empty; if( dbPort != "1433" && !string.IsNullOrEmpty(dbPort) ) sqlConnection = string.Format("server={0},{1};uid={2};pwd={3};database={4}", dbServer, dbPort, dbUserName, dbPassword, dbName); else sqlConnection = string.Format("server={0};uid={1};pwd={2};database={3}", dbServer, dbUserName, dbPassword, dbName); HttpResult hr = new HttpResult() { Result = true }; SqlConnection dbConnection = null; try { dbConnection = new SqlConnection(sqlConnection); dbConnection.Open(); } catch( Exception ex ) { hr.Result = false; hr.ErrorMessage = ex.Message; } finally { if( dbConnection != null ) dbConnection.Close(); } return new JsonResult(hr); }
public JsonResult SaveUser(EsbUsers esbUser) { HttpResult hr = new HttpResult(); hr.Result = true; if( esbUser.UserID == Guid.Empty ) { var parameter = new { UserName = esbUser.UserName}; object obj = CPQuery.From("SELECT 1 FROM esb_Users WHERE UserName = @UserName", parameter).ExecuteScalar<object>(); if( obj != null ) { hr.Result = false; hr.ErrorMessage = "指定用户名已经存在!"; return new JsonResult(hr); } esbUser.UserID = Guid.NewGuid(); esbUser.Insert(); } else { esbUser.Update(); } hr.KeyValue = esbUser.UserID.ToString(); return new JsonResult(hr); }
public JsonResult GetWSDL(string providerID, string relativePath) { HttpResult hr = new HttpResult() { Result = true }; EsbProvider provider = null; try { provider = CPQuery.From("select * from esb_Provider where ProviderID = @ProviderID", new { ProviderID = providerID }).ToSingle<EsbProvider>(); } catch(Exception ex) { hr.Result = false; hr.ErrorMessage = "运行时异常,请与管理员联系!"; new MyLogger().WriteLog(LogLevel.Error, ex.Message, ex.ToString()); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(@"{0}://{1}:{2}{3}?wsdl", provider.Protocol, provider.Domain, provider.Port, relativePath)); request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using(StreamReader sr = new StreamReader(response.GetResponseStream())) { hr.KeyValue = sr.ReadToEnd(); } } return new JsonResult(hr); }
private static IEnumerable<HttpResult> HttpGet(Uri address, int count) { var stopwatch = new Stopwatch(); var results = new HttpResult[count]; for (var i = 0; i < count; i++) { var client = new HttpClient(); var isError = false; var elapsed = 0L; stopwatch.Reset(); try { stopwatch.Start(); client.DownloadString(address); elapsed = stopwatch.ElapsedMilliseconds; } catch (Exception) { elapsed = stopwatch.ElapsedMilliseconds; isError = true; } finally { results[i] = new HttpResult { TimeTakenMs = (int)elapsed, IsError = isError }; } } return results; }
public JsonResult SavePipe(EsbReceiverInterface pipe) { HttpResult hr = new HttpResult() { Result = true }; using (ConnectionScope scope = new ConnectionScope(TransactionMode.Required)){ if (pipe.ReceiverInterfaceID == Guid.Empty){ if (EntityCommon.IsExistData("Esb_ReceiverInterface", "ServiceName", pipe.ServiceName)){ hr.Result = false; hr.ErrorMessage = "英文名称不允许重复,请重新录入英文名称!"; return new JsonResult(hr); } pipe.ReceiverInterfaceID = Guid.NewGuid(); pipe.Status = 1; pipe.CreatedOn = DateTime.Now; pipe.Insert(); //自动生成策略 EsbDistributeStrategy esbDistributeStrategyEntity = new EsbDistributeStrategy(); esbDistributeStrategyEntity.DistributeStrategyID = Guid.NewGuid(); esbDistributeStrategyEntity.ReceiverInterfaceID = pipe.ReceiverInterfaceID; esbDistributeStrategyEntity.CreatedOn = DateTime.Now; esbDistributeStrategyEntity.Status = 1; esbDistributeStrategyEntity.Insert(); } else{ pipe.ModifiedOn = DateTime.Now; pipe.Update(); } hr.KeyValue = pipe.ReceiverInterfaceID.ToString(); scope.Commit(); } return new JsonResult(hr); }
public void Can_send_ResponseStream_test_with_Custom_Header() { var mockResponse = new MockHttpResponse(); var customText = "<h1>Custom Stream</h1>"; var customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, MimeTypes.Html) { Headers = { {"X-Custom","Header"} } }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html); Assert.That(reponseWasAutoHandled.Result, Is.True); var writtenString = mockResponse.ReadAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header")); }
public void CreateInitializesNewPageInstanceTest() { var data = Guid.NewGuid().ToString(); var response = new HttpResponseMessage { Content = new StringContent(data) }; var outcomes = new List<HttpOutcome> { new HttpOutcome( new Uri("http://www.google.com"), HttpMethod.Get, HttpStatusCode.OK, Guid.NewGuid().ToString(), TimeSpan.FromMilliseconds(2)) }; var result = new HttpResult(new ReadOnlyCollection<HttpOutcome>(outcomes)); var browser = Substitute.For<IBrowser>(); var target = new DefaultPageFactory(); var actual = target.Create<TextPageWrapper>(browser, response, result); actual.Should().NotBeNull(); actual.Browser.Should().Be(browser); actual.Result.Should().Be(result); actual.StatusCode.Should().Be(response.StatusCode); actual.StatusDescription.Should().Be(response.ReasonPhrase); actual.Content.Should().Be(data); }
public IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { if (receiveResult != null) throw new InvalidOperationException("Cannot call BeginReceive twice"); receiveResult = new HttpResult(callback, state, buffer, offset, count); try { // BeginReceive has been called *before* we have sent a piece request. // Wait for a piece request to be sent before allowing this to complete. if (dataStream == null) return receiveResult; DoReceive(); return receiveResult; } catch (Exception ex) { if (sendResult != null) sendResult.Complete(ex); if (receiveResult != null) receiveResult.Complete(ex); } return receiveResult; }
public JsonResult SetMainSite(Guid providerGUID) { HttpResult hr = new HttpResult() { Result = true }; var param = new { ProviderID = providerGUID }; CPQuery.From(@"UPDATE dbo.esb_Provider SET IsMainSite=0 UPDATE dbo.esb_Provider SET IsMainSite=1 WHERE ProviderID=@ProviderID", param).ExecuteNonQuery(); return new JsonResult(hr); }
private void DestroyOnCompletion(HttpResult result) { if (!request.CanBeDestroyed()) return; if (gameObject.GetComponents<Component>().Length <= 2) Destroy(gameObject); else Destroy(this); }
public object Get(PartialFromMemory request) { var customText = "123456789012345678901234567890"; var customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, "audio/mpeg"); return httpResult; }
public void Can_handle_null_HttpResult_StatusDescription() { var mockResponse = new MockHttpResponse(); var httpResult = new HttpResult { StatusDescription = null }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html); Assert.That(reponseWasAutoHandled.Result, Is.True); Assert.IsNotNull(mockResponse.StatusDescription); }
public JsonResult DeleteInvoker(string[] invokerGUIDList) { HttpResult hr = new HttpResult() { Result = true }; string keyList = string.Join("','", invokerGUIDList); using( ConnectionScope scope = new ConnectionScope(TransactionMode.Required) ) { CPQuery.From(String.Format("delete from esb_Invoker where InvokerID in ('{0}')", keyList)).ExecuteNonQuery(); scope.Commit(); } return new JsonResult(hr); }
public JsonResult DeleteValidateDistribute(string[] GUIDList) { if( GUIDList == null || GUIDList.Length == 0 ) throw new ArgumentException("接口列表不能为空."); HttpResult hr = new HttpResult() { Result = true }; string keyList = string.Join("','", GUIDList); CPQuery.From(string.Format("delete from esb_VerifyDistribute where VerifyDistributeID in ('{0}')", keyList)).ExecuteNonQuery(); return new JsonResult(hr); }
void HandleImage(HttpResult result) { Texture2D tex = result.texture2d; if (tex == null) { Debug.LogError("Something went wrong!"); return; } Vector3 scale = new Vector3(result.texture2d.width / 100f, result.texture2d.height / 100, 1f); textureHolder.transform.localScale = scale; Material mat = textureHolder.GetComponent<Renderer>().material; mat.mainTexture = tex; mat.shader = Shader.Find("Unlit/Texture"); }
void HandleAudio(HttpResult result) { AudioClip clip = result.audioClip; if (clip == null) { Debug.LogError("Something went wrong!"); return; } if (audioSource == null) { throw new MissingReferenceException(); } audioSource.clip = clip; audioSource.Play(); }
public JsonResult DeletePipe(string[] pipeGUIDList) { if (pipeGUIDList == null || pipeGUIDList.Length == 0) throw new ArgumentException("接收接口列表不能为空."); HttpResult hr = new HttpResult() { Result = true }; string keyList = string.Join("','", pipeGUIDList); using (ConnectionScope scope = new ConnectionScope(TransactionMode.Required)){ CPQuery.From(string.Format("delete from esb_ReceiverInterface where ReceiverInterfaceID in ('{0}')", keyList)).ExecuteNonQuery(); CPQuery.From(string.Format("delete from esb_DistributeStrategy where ReceiverInterfaceID in ('{0}')", keyList)).ExecuteNonQuery(); CPQuery.From(string.Format("delete from esb_DistributeFailedInform where ReceiverInterfaceID in ('{0}')", keyList)).ExecuteNonQuery(); CPQuery.From(string.Format("delete from esb_VerifyDistribute where ReceiverInterfaceID in ('{0}')", keyList)).ExecuteNonQuery(); scope.Commit(); } return new JsonResult(hr); }
public string SaveXml(string dateXml, string userXml) { HttpResult hr = new HttpResult() { Result = true }; try { EsbInvoker esbInvokerEntity = XmlDataEntity.ConvertXmlToSingle<EsbInvoker>(dateXml); EsbUsers user = userXml.FromXml<EsbUsers>(); using (ConnectionScope scope = new ConnectionScope(TransactionMode.Required)) { if (esbInvokerEntity.InvokerID == Guid.Empty) { if (EntityCommon.IsExistData("Esb_Invoker", "InvokerCode", esbInvokerEntity.InvokerCode)) { hr.Result = false; hr.ErrorMessage = "用户代码不允许重复,请重新录入用户代码!"; return hr.ToXml(); } esbInvokerEntity.InvokerID = Guid.NewGuid(); esbInvokerEntity.CreatedOn = DateTime.Now; esbInvokerEntity.CreatedBy = user.UserID; esbInvokerEntity.Insert(); } else { esbInvokerEntity.ModifiedOn = DateTime.Now; esbInvokerEntity.ModifiedBy = user.UserID; esbInvokerEntity.Update(); } hr.KeyValue = esbInvokerEntity.InvokerID.ToString(); scope.Commit(); } } catch (Exception) { hr.Result = false; hr.ErrorMessage = "运行时异常,请与管理员联系!"; } return hr.ToXml(); }
public JsonResult DeleteDB(DelDBArgs arg) { if (arg == null || arg.DbDistributes == null || arg.DbDistributes.Length == 0) throw new ArgumentException("数据库分发接口列表不能为空."); HttpResult hr = new HttpResult() { Result = true }; using (ConnectionScope scope = new ConnectionScope(TransactionMode.Required)){ foreach (string oid in arg.DbDistributes){ var parameter = new{ DbDistributeGUID = new Guid(oid), }; CPQuery.From("DELETE FROM esb_DbDistribute WHERE DbDistributeGUID=@DbDistributeGUID", parameter).ExecuteNonQuery(); } scope.Commit(); } return new JsonResult(hr); }
public void Can_send_ResponseText_test_with_StatusDescription() { var mockRequest = new MockHttpRequest { ContentType = MimeTypes.Json }; var mockResponse = mockRequest.Response; var customStatus = "Custom Status Description"; var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus) { RequestContext = mockRequest }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html); Assert.That(reponseWasAutoHandled.Result, Is.True); var statusDesc = mockResponse.StatusDescription; Assert.That(mockResponse.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.Accepted)); Assert.That(statusDesc, Is.EqualTo(customStatus)); }
public virtual void MakeRequest(string url, string method, System.Action<HttpResult> onResult, System.Func<HttpWebRequest, bool> modifyer = null) { var res = new HttpResult(); HttpWebRequest r = WebRequest.Create(url) as HttpWebRequest; r.Method = method; if (modifyer != null) { modifyer(r); } try { HttpWebResponse response = r.GetResponse() as HttpWebResponse; using (Stream rspStm = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(rspStm)) { res.response = reader.ReadToEnd(); } res.statusCode = (int)response.StatusCode; } } catch (System.Net.WebException ex){ // get error details sent from the server StreamReader reader = new StreamReader(ex.Response.GetResponseStream()); res.response = reader.ReadToEnd(); res.errDescr = ex.Message; res.statusCode = (int)ex.Status; } if (onResult != null) onResult(res); }
private void GetSessionData(HttpResult accessContent) { SessionData = new OAuthSessionInfo(); string[] fragments = accessContent.ResponseUri.Fragment.Substring(1).Split('&'); foreach (string fragment in fragments) { string[] fragmentNameValue = fragment.Split('='); switch (fragmentNameValue[0]) { case "access_token": SessionData.Token = fragmentNameValue[1]; break; case "expires_in": SessionData.Expire = int.Parse(fragmentNameValue[1]); break; case "user_id": SessionData.UserId = int.Parse(fragmentNameValue[1]); break; } } }
public void Can_send_ResponseText_test_with_Custom_Header() { var mockResponse = new MockHttpResponse(); var customText = "<h1>Custom Text</h1>"; var httpResult = new HttpResult(customText, MimeTypes.Html) { Headers = { {"X-Custom","Header"} } }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html); Assert.That(reponseWasAutoHandled.Result, Is.True); var writtenString = mockResponse.ReadAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header")); }
public JsonResult DeleteUsers(string keyList) { HttpResult hr = new HttpResult() { Result = true }; keyList = keyList.Replace(";", "','"); try { using (ConnectionScope scope = new ConnectionScope(TransactionMode.Required)) { CPQuery.From(String.Format("delete from esb_Users where UserID in ('{0}')", keyList)).ExecuteNonQuery(); CPQuery.From(String.Format("delete from esb_DistributeFailedInform where UserID in ('{0}')", keyList)).ExecuteNonQuery(); scope.Commit(); } } catch { hr.Result = false; hr.ErrorMessage = "删除用户失败!"; } return new JsonResult(hr); }
/// <summary> /// 快速Post数据这个访求与GetHtml一样,只是不接收返回数据,只做提交。 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult FastRequest(HttpItem item) { //返回参数 HttpResult result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { //配置参数时出错 return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }; } try { //请求数据 using (response = (HttpWebResponse)request.GetResponse()) { //成功 不做处理只回成功状态 return new HttpResult() { Cookie = string.Empty, Header = response.Headers, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription }; } } catch (WebException ex) { using (response = (HttpWebResponse)ex.Response) { //不做处理只回成功状态 return new HttpResult() { Cookie = string.Empty, Header = response.Headers, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription }; } } catch (Exception ex) { result.Html = ex.Message; } if (item.IsToLower) result.Html = result.Html.ToLower(); return result; }
void NextTest(HttpResult result) { if (currentTestIndex < testRequests.Count) { string message = "Passing test #: "; if (failingTestStartIndex > 0 && currentTestIndex >= failingTestStartIndex) message = "Failing test #: "; message += currentTestIndex.ToString(); Debug.LogWarning(message); request = testRequests[currentTestIndex]; request.onSuccess += OnSuccess; request.onError += OnFailure; request.timeOut = 2f; request.retryAttempts = 2; request.onFinished += NextTest; request.Execute(); currentTestIndex++; } else Debug.LogWarning("TESTING COMPLETE"); }
private void BeginRequest(HttpWebRequest request, System.Action<HttpResult> onResult) { //int total = 0; //byte[] buffer = new byte[1000]; request.BeginGetResponse(new AsyncCallback((result) => { var data = request.EndGetResponse(result); HttpResult r = new HttpResult(); try { var stream = new StreamReader(data.GetResponseStream()); r.response = stream.ReadToEnd(); stream.Close(); //TODO:!! /*stream.BeginRead(buffer, 0, 1000, new AsyncCallback((read) => { var r = stream.EndRead(read); if (r == 0) { //TODO: !! } else { //stream.BeginRead } }));*/ } catch (Exception ex) { r.errDescr = ex.Message; } finally { data.Close(); } if (onResult != null) { onResult(r); } }), null); }
public static HttpResult ToHttpResult <TValue>(this HttpResult <TValue> result) { return(result.IsSuccess ? HttpResult.Ok(result.HttpState) : HttpResult.Fail(result.HttpState)); }
/// <summary> /// 创建块(携带首片数据),v1检查CRC32,v2检查md5 /// </summary> /// <param name="resumeBlockerObj">创建分片上次的块请求</param> private void MakeBlock(object resumeBlockerObj) { ResumeBlocker resumeBlocker = (ResumeBlocker)resumeBlockerObj; ManualResetEvent doneEvent = resumeBlocker.DoneEvent; Dictionary <long, HttpResult> blockMakeResults = resumeBlocker.BlockMakeResults; PutExtra putExtra = resumeBlocker.PutExtra; long blockIndex = resumeBlocker.BlockIndex; HttpResult result = new HttpResult(); //check whether to cancel while (true) { UploadControllerAction upCtl = resumeBlocker.PutExtra.UploadController(); if (upCtl == UploadControllerAction.Suspended) { doneEvent.WaitOne(1000); continue; } else if (upCtl == UploadControllerAction.Aborted) { doneEvent.Set(); result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += string.Format("[{0}] [ResumableUpload] Info: upload task is aborted, mkblk {1}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), blockIndex); blockMakeResults.Add(blockIndex, result); return; } else { break; } } byte[] blockBuffer = resumeBlocker.BlockBuffer; int blockSize = blockBuffer.Length; string upToken = resumeBlocker.UploadToken; Dictionary <string, long> uploadedBytesDict = resumeBlocker.UploadedBytesDict; long fileSize = resumeBlocker.FileSize; object progressLock = resumeBlocker.ProgressLock; ResumeInfo resumeInfo = resumeBlocker.ResumeInfo; try { //get upload host string ak = UpToken.GetAccessKeyFromUpToken(upToken); string bucket = UpToken.GetBucketFromUpToken(upToken); if (ak == null || bucket == null) { result = HttpResult.InvalidToken; doneEvent.Set(); return; } string uploadHost = this.config.UpHost(ak, bucket); string url = ""; if (putExtra.Version == "v1") { url = string.Format("{0}/mkblk/{1}", uploadHost, blockSize); } else { url = string.Format("{0}/buckets/{1}/objects/{2}/uploads/{3}/{4}", uploadHost, bucket, resumeBlocker.encodedObjectName, resumeInfo.UploadId, blockIndex + 1); } string upTokenStr = string.Format("UpToken {0}", upToken); using (MemoryStream ms = new MemoryStream(blockBuffer, 0, blockSize)) { byte[] data = ms.ToArray(); if (putExtra.Version == "v1") { result = httpManager.PostData(url, data, upTokenStr); } else { Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("Authorization", upTokenStr); // data to md5 string md5 = LabMD5.GenerateMD5(blockBuffer); headers.Add("Content-MD5", md5); result = httpManager.PutDataWithHeaders(url, data, headers); } if (result.Code == (int)HttpCode.OK) { if (putExtra.Version == "v1") { ResumeContext rc = JsonConvert.DeserializeObject <ResumeContext>(result.Text); if (rc.Crc32 > 0) { uint crc_1 = rc.Crc32; uint crc_2 = CRC32.CheckSumSlice(blockBuffer, 0, blockSize); if (crc_1 != crc_2) { result.RefCode = (int)HttpCode.USER_NEED_RETRY; result.RefText += string.Format(" CRC32: remote={0}, local={1}\n", crc_1, crc_2); } else { //write the mkblk context resumeInfo.Contexts[blockIndex] = rc.Ctx; resumeInfo.ExpiredAt = rc.ExpiredAt; lock (progressLock) { uploadedBytesDict["UploadProgress"] += blockSize; } putExtra.ProgressHandler(uploadedBytesDict["UploadProgress"], fileSize); } } else { result.RefText += string.Format("[{0}] JSON Decode Error: text = {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), result.Text); result.RefCode = (int)HttpCode.USER_NEED_RETRY; } } else { Dictionary <string, string> rc = JsonConvert.DeserializeObject <Dictionary <string, string> >(result.Text); string md5 = LabMD5.GenerateMD5(blockBuffer); if (md5 != rc["md5"]) { result.RefCode = (int)HttpCode.USER_NEED_RETRY; result.RefText += string.Format(" md5: remote={0}, local={1}\n", rc["md5"], md5); } else { Dictionary <string, object> etag = new Dictionary <string, object>(); etag.Add("etag", rc["etag"]); etag.Add("partNumber", blockIndex + 1); resumeInfo.Etags[blockIndex] = etag; lock (progressLock) { uploadedBytesDict["UploadProgress"] += blockSize; resumeInfo.Uploaded += blockSize; } putExtra.ProgressHandler(uploadedBytesDict["UploadProgress"], fileSize); } } } else { result.RefCode = (int)HttpCode.USER_NEED_RETRY; } } } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}] mkblk Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); Exception e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException) { QiniuException qex = (QiniuException)ex; result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } //return the http result blockMakeResults.Add(blockIndex, result); doneEvent.Set(); }
public object Get(PlainTextRequest request) { var response = new HttpResult("Hello, World!", "text/plain"); return(response); }
public async Task Unauthorized() { HttpResult <UserInfo> result = await client.GetUserInfoAsync(); Assert.AreEqual(result.StatusCode, HttpStatusCode.Unauthorized); }
public override bool GetOrderManager(string requestData, ref OrderManager orderManger) { try { if (orderManger == null) { m_lastError = "获得预约订单管理信息失败:预约订单管理对象为null"; Logger.WriteError(m_lastError); return(false); } m_httpItem.URL = requestData; m_httpItem.ResultType = ResultType.String; m_httpItem.Method = "GET"; m_httpItem.Postdata = string.Empty; m_httpItem.ContentType = "text/html"; m_httpItem.Allowautoredirect = false; HttpResult html = m_httpHelper.GetHtml(m_httpItem); if (html.RedirectUrl.Length == 0) { m_htmlDocument.LoadHtml(html.Html); HtmlNode htmlNode = m_htmlDocument.DocumentNode.SelectSingleNode(ORDER_INFO_XPATH); if (htmlNode != null) { HtmlNodeCollection htmlNodeCollection = htmlNode.SelectNodes("div[@class='m_l']/div[@class='m_l_1']/div[@class='con_r']/p"); if (htmlNodeCollection != null && htmlNodeCollection.Count == 3) { orderManger.Doctor.Name = htmlNodeCollection[0].InnerHtml.Trim(); orderManger.Doctor.HospitalName = htmlNodeCollection[2].InnerHtml.Trim(); } HtmlNode htmlNode2 = htmlNode.SelectSingleNode("input[@name='hosId']"); if (htmlNode2 != null) { orderManger.Doctor.HospitalId = htmlNode2.GetAttributeValue("value", ""); } HtmlNodeCollection htmlNodeCollection2 = htmlNode.SelectNodes("input"); if (htmlNodeCollection2 != null && htmlNodeCollection2.Count == 14) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("numId={0}&resTime={1}&resNumber={2}&"); for (int i = 3; i < 14; i++) { stringBuilder.Append(htmlNodeCollection2[i].GetAttributeValue("name", "")); stringBuilder.Append("="); stringBuilder.Append(htmlNodeCollection2[i].GetAttributeValue("value", "")); stringBuilder.Append("&"); } stringBuilder.Append("num={3}"); orderManger.CheckOrderPostUrlFormat = stringBuilder.ToString(); } HtmlNodeCollection htmlNodeCollection3 = htmlNode.SelectNodes("div[@class='m_l']/div[@class='m_l_2']/p/span"); if (htmlNodeCollection3 != null && htmlNodeCollection3.Count == 3) { orderManger.RegisterFee = htmlNodeCollection3[0].InnerHtml.Trim(); orderManger.Doctor.Department = htmlNodeCollection3[1].InnerHtml.Trim(); orderManger.RegisterDate = htmlNodeCollection3[2].InnerHtml.Trim().Replace(" ", " "); } HtmlNodeCollection htmlNodeCollection4 = htmlNode.SelectNodes("div[@class='m_r']/div[@class='m_r_1']/table/tr/td/input"); if (htmlNodeCollection4 == null) { m_lastError = "获得预约订单管理信息失败:预约订单信息解析失败"; Logger.WriteError(m_lastError); return(false); } using (IEnumerator <HtmlNode> enumerator = ((IEnumerable <HtmlNode>)htmlNodeCollection4).GetEnumerator()) { while (enumerator.MoveNext()) { HtmlNode current = enumerator.Current; string[] array = current.GetAttributeValue("value", "").Split(new char[] { ',' }); if (array != null && array.Length == 3) { OrderInfo orderInfo = new OrderInfo(); orderInfo.OrderNum = array[2]; orderInfo.OrderId = array[0]; orderInfo.OrderTime = array[1]; orderManger.OrderInfos.Add(orderInfo); } } } } } else { m_lastError = "获得预约订单管理信息失败:用户未登录"; Logger.WriteError(m_lastError); return(false); } } catch (Exception err) { m_lastError = "获得预约订单管理信息失败:" + err.Message; Logger.WriteError("获得预约订单管理信息失败", err); return(false); } return(true); }
public QiniuException(HttpResult httpResult, string message) { this.HttpResult = httpResult == null ? new HttpResult() : httpResult; this.message = message; }
private void SomeMethod() { var s = new HttpResult("", ""); // not a sink }
public object Any(ClearChatHistory request) { ChatHistory.Flush(); return(HttpResult.Redirect("/")); }
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) { var tokens = this.Init(authService, ref session, request); var authServer = new AuthorizationServerDescription { AuthorizationEndpoint = new Uri(this.AuthorizeUrl), TokenEndpoint = new Uri(this.AccessTokenUrl) }; var authClient = new WebServerClient(authServer, this.ConsumerKey) { ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(this.ConsumerSecret), }; var authState = ProcessUserAuthorization(authClient, authServer, authService); if (authState == null) { try { var authReq = authClient.PrepareRequestUserAuthorization(this.Scopes, new Uri(this.CallbackUrl)); var authContentType = authReq.Headers[HttpHeaders.ContentType]; var httpResult = new HttpResult(authReq.ResponseStream, authContentType) { StatusCode = authReq.Status, StatusDescription = "Moved Temporarily" }; foreach (string header in authReq.Headers) { httpResult.Headers[header] = authReq.Headers[header]; } foreach (string name in authReq.Cookies) { var cookie = authReq.Cookies[name]; if (cookie != null) { httpResult.SetSessionCookie(name, cookie.Value, cookie.Path); } } authService.SaveSession(session, this.SessionExpiry); return(httpResult); } catch (ProtocolException ex) { Log.Error("Failed to login to {0}".Fmt(this.Provider), ex); return(authService.Redirect(session.ReferrerUrl.AddHashParam("f", "Unknown"))); } } var accessToken = authState.AccessToken; if (accessToken != null) { try { tokens.AccessToken = accessToken; tokens.RefreshToken = authState.RefreshToken; tokens.RefreshTokenExpiry = authState.AccessTokenExpirationUtc; session.IsAuthenticated = true; var authInfo = this.CreateAuthInfo(accessToken); this.OnAuthenticated(authService, session, tokens, authInfo); return(authService.Redirect(session.ReferrerUrl.AddHashParam("s", "1"))); } catch (WebException we) { var statusCode = ((HttpWebResponse)we.Response).StatusCode; if (statusCode == HttpStatusCode.BadRequest) { return(authService.Redirect(session.ReferrerUrl.AddHashParam("f", "AccessTokenFailed"))); } } } return(authService.Redirect(session.ReferrerUrl.AddHashParam("f", "RequestTokenFailed"))); }
public object Post(Authenticate request) { AssertAuthProviders(); if (ValidateFn != null) { var validationResponse = ValidateFn(this, Request.Verb, request); if (validationResponse != null) { return(validationResponse); } } if (request.RememberMe.HasValue) { var opt = request.RememberMe.GetValueOrDefault(false) ? SessionOptions.Permanent : SessionOptions.Temporary; base.Request.AddSessionOptions(opt); } var provider = request.provider ?? AuthProviders[0].Provider; if (provider == CredentialsAliasProvider) { provider = CredentialsProvider; } var authProvider = GetAuthProvider(provider); if (authProvider == null) { throw HttpError.NotFound(ErrorMessages.UnknownAuthProviderFmt.Fmt(provider)); } if (LogoutAction.EqualsIgnoreCase(request.provider)) { return(authProvider.Logout(this, request)); } var authWithRequest = authProvider as IAuthWithRequest; if (authWithRequest != null && !base.Request.IsInProcessRequest()) { //IAuthWithRequest normally doesn't call Authenticate directly, but they can to return Auth Info //But as AuthenticateService doesn't have [Authenticate] we need to call it manually new AuthenticateAttribute().Execute(base.Request, base.Response, request); if (base.Response.IsClosed) { return(null); } } var session = this.GetSession(); var isHtml = base.Request.ResponseContentType.MatchesContentType(MimeTypes.Html); try { var response = Authenticate(request, provider, session, authProvider); // The above Authenticate call may end an existing session and create a new one so we need // to refresh the current session reference. session = this.GetSession(); if (request.provider == null && !session.IsAuthenticated) { throw HttpError.Unauthorized(ErrorMessages.NotAuthenticated); } var referrerUrl = request.Continue ?? session.ReferrerUrl ?? this.Request.GetHeader("Referer") ?? authProvider.CallbackUrl; var alreadyAuthenticated = response == null; response = response ?? new AuthenticateResponse { UserId = session.UserAuthId, UserName = session.UserAuthName, DisplayName = session.DisplayName ?? session.UserName ?? "{0} {1}".Fmt(session.FirstName, session.LastName).Trim(), SessionId = session.Id, ReferrerUrl = referrerUrl, }; var authResponse = response as AuthenticateResponse; if (authResponse != null) { foreach (var responseFilter in AuthResponseFilters) { authResponse = responseFilter.Execute(this, authProvider, session, authResponse) ?? authResponse; } if (AuthResponseDecorator != null) { return(AuthResponseDecorator(this, request, authResponse)); } } if (isHtml && request.provider != null) { if (alreadyAuthenticated) { return(this.Redirect(referrerUrl.SetParam("s", "0"))); } if (!(response is IHttpResult) && !string.IsNullOrEmpty(referrerUrl)) { return(new HttpResult(response) { Location = referrerUrl }); } } return(response); } catch (HttpError ex) { var errorReferrerUrl = this.Request.GetHeader("Referer"); if (isHtml && errorReferrerUrl != null) { errorReferrerUrl = errorReferrerUrl.SetParam("f", ex.Message.Localize(Request)); return(HttpResult.Redirect(errorReferrerUrl)); } throw; } }
public override async Task <object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = default) { var tokens = Init(authService, ref session, request); var ctx = CreateAuthContext(authService, session, tokens); var httpReq = authService.Request; var isOpenIdRequest = !httpReq.GetParam("openid.mode").IsNullOrEmpty(); if (!isOpenIdRequest) { var openIdUrl = httpReq.GetParam("OpenIdUrl") ?? base.AuthRealm; if (openIdUrl.IsNullOrEmpty()) { throw new ArgumentException("'OpenIdUrl' is required a required field"); } try { using (var openid = CreateOpenIdRelyingParty(OpenIdApplicationStore)) { var openIdRequest = openid.CreateRequest(openIdUrl); AddAttributeExchangeExtensions(openIdRequest); // This is where you would add any OpenID extensions you wanted // to include in the authentication request. openIdRequest.AddExtension(CreateClaimsRequest(httpReq)); // Send your visitor to their Provider for authentication. var openIdResponse = openIdRequest.RedirectingResponse; var contentType = openIdResponse.Headers[HttpHeaders.ContentType]; var httpResult = new HttpResult(openIdResponse.ResponseStream, contentType) { StatusCode = openIdResponse.Status, StatusDescription = "Moved Temporarily", }; foreach (string header in openIdResponse.Headers) { httpResult.Headers[header] = openIdResponse.Headers[header]; } // Save the current session to keep the ReferrerUrl available (similar to Facebook provider) await this.SaveSessionAsync(authService, session, SessionExpiry, token).ConfigAwait(); return(httpResult); } } catch (ProtocolException ex) { Log.Error("Failed to login to {0}".Fmt(openIdUrl), ex); return(authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "Unknown")))); } } if (isOpenIdRequest) { using (var openid = CreateOpenIdRelyingParty(OpenIdApplicationStore)) { var response = openid.GetResponse(); if (response != null) { switch (response.Status) { case AuthenticationStatus.Authenticated: var authInfo = CreateAuthInfo(response); // Use FormsAuthentication to tell ASP.NET that the user is now logged in, // with the OpenID Claimed Identifier as their username. session.IsAuthenticated = true; return(await OnAuthenticatedAsync(authService, session, tokens, authInfo, token).ConfigAwait() ?? authService.Redirect(SuccessRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("s", "1")))); //Haz access! case AuthenticationStatus.Canceled: return(authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "ProviderCancelled")))); case AuthenticationStatus.Failed: return(authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "Unknown")))); } } } } //Shouldn't get here return(authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "Unknown")))); }
/// <summary> /// 抓取指数 /// </summary> public void ExceuteCrawIndex() { try { List <string> keywordsList = new List <string>(); ////从文本读取关键词 using (System.IO.StreamReader sr = new System.IO.StreamReader("F:\\amekeyword.txt", Encoding.GetEncoding("GB2312"))) { string str; while ((str = sr.ReadLine()) != null) { keywordsList.Add(str); } } MessagePipe.ExcuteWriteMessageEvent("取到关键词" + keywordsList.Count + "条", 0); ////开始遍历关键词 foreach (string keyword in keywordsList) { try { MessagePipe.ExcuteWriteMessageEvent("开始处理关键词【" + keyword + "】", 0); HttpRequestParam param = new HttpRequestParam(); string tempword = System.Web.HttpUtility.UrlEncode(keyword, System.Text.Encoding.GetEncoding("GB2312")); tempword = tempword.ToUpper(); param.URL = string.Format("http://index.baidu.com/?tpl=crowd&word={0}", tempword); param.Method = "get"; param.AllowAutoRedirect = false; param.IsIEProxy = true; param.Cookie = this.cookie; param.Timeout = 7 * 1000; param.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"; param.ResultType = ResultType.String; HttpResult result = HttpHelper.GetHttpRequestData(param); String temphtml = result.Html; temphtml = Regex.Replace(temphtml, @"\r|\n|\s|\t", string.Empty, RegexOptions.IgnoreCase); Regex regextemp = new Regex("PPval.ppt=\'(?<str>.*?)\',"); Match matchresult = regextemp.Match(temphtml); string tempstr = matchresult.Groups["str"].Value; tempstr += "&res2="; ////拼凑字符串,进行解析 string temprefer = param.URL; param.Referer = temprefer; param.URL = string.Format("http://index.baidu.com/Interface/Social/getSocial/?res={0}", tempstr); param.Header.Add("X-Requested-With", "XMLHttpRequest"); result = HttpHelper.GetHttpRequestData(param); string jsonstr = result.Html; jsonstr = jsonstr.Replace("\"", string.Empty); regextemp = new Regex("str_age:\\{(?<age>.*?)\\},str_sex:\\{(?<sex>.*?)\\}"); matchresult = regextemp.Match(jsonstr); string ageregion = matchresult.Groups["age"].Value; string sexstr = matchresult.Groups["sex"].Value; List <string> agelist = ageregion.Split(',').ToList(); List <string> sexlist = sexstr.Split(',').ToList(); ////解析数据 string content = string.Empty; foreach (string tempage in agelist) { List <string> tempageList = tempage.Split(':').ToList(); content += tempageList[1] + " "; } foreach (string tempsex in sexlist) { List <string> tempsexlist = tempsex.Split(':').ToList(); content += tempsexlist[1] + " "; } ////追加到txt WriteTxt.WriteAppendTxt("F:\\\baidu.txt", content); MessagePipe.ExcuteWriteMessageEvent("关键词【" + keyword + "】指数数据添加" + content, 0); } catch (Exception ex) { MessagePipe.ExcuteWriteMessageEvent("处理关键词【" + keyword + "】发生异常:" + ex.Message, 1); } } } catch (Exception ex) { } }
/// <summary> /// Разбор HTTP-ответа службы /// </summary> private static Response ProcessResponse(HttpResult reply) { var response = new Response { StatusCode = reply.Code }; var contentType = reply.ContentType; var ctFields = contentType.Split(';'); var binary = ctFields[0] == "multipart/related"; if (binary) { string boundary = null; for (var i = 1; i < ctFields.Length; i++) { var field = ctFields[i].Trim(' ').Split('='); if (field[0] == "boundary") { boundary = field[1].Trim('\"'); break; } } if (boundary == null) { throw new ApplicationException("Multipart boundary не найден в заголовке Content-Type"); } var respData = reply.Content; var pos = 0; using (var stream = new MemoryStream(respData)) { using (var reader = new StreamReader(stream, Encoding.ASCII)) { Func<string> readLine = () => { // ReSharper disable once AccessToDisposedClosure var line = reader.ReadLine(); if (line != null) { pos += line.Length + 2; } return line; }; Action goToEmpty = () => { string line; do { line = readLine(); } while (!string.IsNullOrWhiteSpace(line)); }; goToEmpty(); var xmlString = readLine(); response.Xml.LoadXml(xmlString); goToEmpty(); } } var length = respData.Length - (pos + boundary.Length + 6); var rData = new byte[length]; Buffer.BlockCopy(respData, pos, rData, 0, length); response.Data = rData; } else { response.Xml.LoadXml(Encoding.UTF8.GetString(reply.Content)); } return response; }
//================= 选择登录方式 ===================== #region 游戏登陆方式 #region 1、QuestLogin 游客注册并登录 /// <summary> /// 游客注册并登录 /// </summary> /// <param name="loginType">登录方式</param> /// <param name="timeAction">时间代理</param> public static async void QuestLogin(LoginType loginType, TimeAction timeAction) { Dictionary <string, object> dic = VarDicSO.NewValue(); dic["AUTHKEY"] = ""; try { //游客模式登陆 第一次登陆,用户和密码为空\第二次登陆之后 根据 UUID 和 授权码进行自动登陆 PlayerInfo playerInfo = SettingComponent.This.GetObject <PlayerInfo>(GlobalData.CookieSets.Cookie_Quick); if (playerInfo != null) { dic["AUTHKEY"] = playerInfo.AuthorizedSKey; } WebRequestAwaitEventArgs webRequest = await WebRequestLogin(dic, loginType); if (webRequest.WebRequestCallBackStatus == WebRequestCallBackStatus.Success) { string responseJson = Utility.Converter.GetString(webRequest.GetWebResponseBytes()); HttpResult ret = JsonHelper.ToObject <HttpResult>(responseJson); //返回登录成功or失败信息 P6_LoginMesg.This.m_Text_LoginMesg .SetGTextByCode(ret.code.ToString()); if (!ret.status) { Log.Error(ret.code.ToString() + responseJson, LogCategory.Proto); //执行结束 结束异步时间代理调用 timeAction?.EndLoop(); } else { timeAction.CFParams.BoolParam1 = true; //关闭用户信息面板 Login_Account.PageToAccount_Close(); Log.Info(ret.code.ToString() + responseJson, LogCategory.Proto); //获取到网关信息 LoginFromRealmCallback(ret.data?.ToString(), loginType, timeAction); } } else { P6_LoginMesg.This .m_Text_LoginMesg .SetGTextByValue(webRequest.ErrorMessage); Log.Info(webRequest.ErrorMessage, LogCategory.Proto); //执行结束 结束异步时间代理调用 timeAction?.EndLoop(); } } catch (Exception ex) { Log.Error(ex.Message, LogCategory.Proto); } }
/// <summary> /// 分片上传/断点续上传,带有自定义进度处理和上传控制,检查CRC32,可自动重试 /// </summary> /// <param name="stream">待上传文件流</param> /// <param name="key">要保存的文件名称</param> /// <param name="upToken">上传凭证</param> /// <param name="putExtra">可选配置参数</param> /// <returns>上传文件后返回结果</returns> public async Task <HttpResult> UploadStream(Stream stream, string key, string upToken, PutExtra putExtra) { var result = new HttpResult(); //check put extra if (putExtra == null) { putExtra = new PutExtra(); } if (putExtra.ProgressHandler == null) { putExtra.ProgressHandler = DefaultUploadProgressHandler; } if (putExtra.UploadController == null) { putExtra.UploadController = DefaultUploadController; } if (!(putExtra.BlockUploadThreads > 0 && putExtra.BlockUploadThreads <= 64)) { putExtra.BlockUploadThreads = 1; } using (stream) { //start to upload try { long uploadedBytes = 0; var fileSize = stream.Length; var blockCount = (fileSize + BlockSize - 1) / BlockSize; //check resume record file ResumeInfo resumeInfo = null; if (File.Exists(putExtra.ResumeRecordFile)) { resumeInfo = ResumeHelper.Load(putExtra.ResumeRecordFile); if (resumeInfo != null && fileSize == resumeInfo.FileSize) { //check whether ctx expired if (UnixTimestamp.IsContextExpired(resumeInfo.ExpiredAt)) { resumeInfo = null; } } } if (resumeInfo == null) { resumeInfo = new ResumeInfo { FileSize = fileSize, BlockCount = blockCount, Contexts = new string[blockCount], ExpiredAt = 0 }; } //calc upload progress for (long blockIndex = 0; blockIndex < blockCount; blockIndex++) { var context = resumeInfo.Contexts[blockIndex]; if (!string.IsNullOrEmpty(context)) { uploadedBytes += BlockSize; } } //set upload progress putExtra.ProgressHandler(uploadedBytes, fileSize); //init block upload error //check not finished blocks to upload var upCtrl = putExtra.UploadController(); var manualResetEvent = new ManualResetEvent(false); var blockDataDict = new Dictionary <long, byte[]>(); var blockMakeResults = new Dictionary <long, HttpResult>(); var uploadedBytesDict = new Dictionary <string, long> { { "UploadProgress", uploadedBytes } }; var blockBuffer = new byte[BlockSize]; for (long blockIndex = 0; blockIndex < blockCount; blockIndex++) { var context = resumeInfo.Contexts[blockIndex]; if (string.IsNullOrEmpty(context)) { //check upload controller action before each chunk while (true) { upCtrl = putExtra.UploadController(); if (upCtrl == UploadControllerAction.Aborted) { result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Info: upload task is aborted\n"; manualResetEvent.Set(); return(result); } if (upCtrl == UploadControllerAction.Suspended) { result.RefCode = (int)HttpCode.USER_PAUSED; result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Info: upload task is paused\n"; manualResetEvent.WaitOne(1000); } else if (upCtrl == UploadControllerAction.Activated) { break; } } var offset = blockIndex * BlockSize; stream.Seek(offset, SeekOrigin.Begin); var blockLen = stream.Read(blockBuffer, 0, BlockSize); var blockData = new byte[blockLen]; Array.Copy(blockBuffer, blockData, blockLen); blockDataDict.Add(blockIndex, blockData); if (blockDataDict.Count == putExtra.BlockUploadThreads) { ProcessMakeBlocks(blockDataDict, upToken, putExtra, resumeInfo, blockMakeResults, uploadedBytesDict, fileSize); //check mkblk results foreach (var blkIndex in blockMakeResults.Keys) { var mkblkRet = blockMakeResults[blkIndex]; if (mkblkRet.Code != 200) { result = mkblkRet; manualResetEvent.Set(); return(result); } } blockDataDict.Clear(); blockMakeResults.Clear(); if (!string.IsNullOrEmpty(putExtra.ResumeRecordFile)) { ResumeHelper.Save(resumeInfo, putExtra.ResumeRecordFile); } } } } if (blockDataDict.Count > 0) { ProcessMakeBlocks(blockDataDict, upToken, putExtra, resumeInfo, blockMakeResults, uploadedBytesDict, fileSize); //check mkblk results foreach (var blkIndex in blockMakeResults.Keys) { var mkblkRet = blockMakeResults[blkIndex]; if (mkblkRet.Code != 200) { result = mkblkRet; manualResetEvent.Set(); return(result); } } blockDataDict.Clear(); blockMakeResults.Clear(); if (!string.IsNullOrEmpty(putExtra.ResumeRecordFile)) { ResumeHelper.Save(resumeInfo, putExtra.ResumeRecordFile); } } if (upCtrl == UploadControllerAction.Activated) { var hr = await MakeFile(key, fileSize, key, upToken, putExtra, resumeInfo.Contexts); if (hr.Code != (int)HttpCode.OK) { result.Shadow(hr); result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Error: mkfile: code = {hr.Code}, text = {hr.Text}\n"; } if (File.Exists(putExtra.ResumeRecordFile)) { File.Delete(putExtra.ResumeRecordFile); } result.Shadow(hr); result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Uploaded: \"{putExtra.ResumeRecordFile}\" ==> \"{key}\"\n"; } else { result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Info: upload task is aborted, mkfile\n"; } manualResetEvent.Set(); return(result); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); var sb = new StringBuilder(); sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Error: "); var e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } return(result); }
public void uploadFile(object obj) { FileItem item = obj as FileItem; if (syncProgressPage.checkCancelSignal()) { this.doneEvent.Set(); return; } string fileFullPath = item.LocalFile; if (!File.Exists(fileFullPath)) { Log.Error(string.Format("file not found error, {0}", fileFullPath)); this.doneEvent.Set(); return; } //set upload params int putThreshold = this.syncSetting.ChunkUploadThreshold; int chunkSize = this.syncSetting.DefaultChunkSize; bool uploadFromCDN = this.syncSetting.UploadFromCDN; string myDocPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string recordPath = System.IO.Path.Combine(myDocPath, "qsunsync", "resume"); if (!Directory.Exists(recordPath)) { Directory.CreateDirectory(recordPath); } Mac mac = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY); //current file info FileInfo fileInfo = new FileInfo(fileFullPath); long fileLength = fileInfo.Length; string fileLastModified = fileInfo.LastWriteTimeUtc.ToFileTime().ToString(); //support resume upload string recorderKey = string.Format("{0}:{1}:{2}:{3}:{4}", this.syncSetting.LocalDirectory, this.syncSetting.TargetBucket, item.SaveKey, fileFullPath, fileLastModified); recorderKey = Hashing.CalcMD5X(recorderKey); this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath); PutPolicy putPolicy = new PutPolicy(); if (this.syncSetting.OverwriteDuplicate) { putPolicy.Scope = this.syncSetting.TargetBucket + ":" + item.SaveKey; } else { putPolicy.Scope = this.syncSetting.TargetBucket; } putPolicy.SetExpires(24 * 30 * 3600); string uptoken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath); HttpResult result = null; ChunkUnit cu = (ChunkUnit)(chunkSize / (128 * 1024)); if (item.Length > putThreshold) { ResumableUploader ru = new ResumableUploader(uploadFromCDN, cu); string recordFile = System.IO.Path.Combine(recordPath, Hashing.CalcMD5X(fileFullPath)); UploadProgressHandler upph = new UploadProgressHandler(delegate(long uploaded, long total) { this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, item.SaveKey, uploaded, fileLength); }); result = ru.UploadFile(fileFullPath, item.SaveKey, uptoken, recordFile, upph, upController); } else { FormUploader su = new FormUploader(uploadFromCDN); result = su.UploadFile(fileFullPath, item.SaveKey, uptoken); } if (result.Code == (int)HttpCode.OK) { this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath); this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, fileFullPath, item.SaveKey)); this.syncProgressPage.updateTotalUploadProgress(); } else { this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + result.Text); this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.TargetBucket, fileFullPath, item.SaveKey, result.Text)); } this.doneEvent.Set(); }
/// <summary> /// 根据已上传的所有分片数据创建文件 /// </summary> /// <param name="fileName">源文件名</param> /// <param name="size">文件大小</param> /// <param name="key">要保存的文件名</param> /// <param name="contexts">所有数据块的Context</param> /// <param name="upToken">上传凭证</param> /// <param name="putExtra">用户指定的额外参数</param> /// <returns>此操作执行后的返回结果</returns> private async Task <HttpResult> MakeFile(string fileName, long size, string key, string upToken, PutExtra putExtra, string[] contexts) { var result = new HttpResult(); try { var fnameStr = "fname"; var mimeTypeStr = ""; var keyStr = ""; var paramStr = ""; //check file name if (!string.IsNullOrEmpty(fileName)) { fnameStr = $"/fname/{Base64.UrlSafeBase64Encode(fileName)}"; } //check mime type if (!string.IsNullOrEmpty(putExtra.MimeType)) { mimeTypeStr = $"/mimeType/{Base64.UrlSafeBase64Encode(putExtra.MimeType)}"; } //check key if (!string.IsNullOrEmpty(key)) { keyStr = $"/key/{Base64.UrlSafeBase64Encode(key)}"; } //check extra params if (putExtra.Params != null && putExtra.Params.Count > 0) { var sb = new StringBuilder(); foreach (var kvp in putExtra.Params) { var k = kvp.Key; var v = kvp.Value; if (k.StartsWith("x:") && !string.IsNullOrEmpty(v)) { sb.Append($"/{k}/{v}"); } } paramStr = sb.ToString(); } //get upload host var ak = UpToken.GetAccessKeyFromUpToken(upToken); var bucket = UpToken.GetBucketFromUpToken(upToken); if (ak == null || bucket == null) { return(HttpResult.InvalidToken); } var uploadHost = await _config.UpHost(ak, bucket); var url = $"{uploadHost}/mkfile/{size}{mimeTypeStr}{fnameStr}{keyStr}{paramStr}"; var body = string.Join(",", contexts); var upTokenStr = $"UpToken {upToken}"; result = await _httpManager.PostTextAsync(url, body, upTokenStr); } catch (Exception ex) { var sb = new StringBuilder(); sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] mkfile Error: "); var e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException qex) { result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } return(result); }
public static HttpResult loadSockerStream(Stream stream, String encode, Int32 resultType) { try { Encoding encoder = Encoding.GetEncoding(encode); //接受Header String line = readLine(stream, encoder); HttpResult httpResult = new HttpResult(); if (resultType == 0) { httpResult.Header = line; return(httpResult); } StringBuilder result = new StringBuilder(); while (!String.IsNullOrEmpty(line) && !line.Equals("\r\n")) { result.Append(line); line = readLine(stream, encoder); } httpResult.Header = result.ToString(); if (resultType == 1) { return(httpResult); } if (resultType == 2) { result = new StringBuilder(); byte[] bteReceive = null; //接受Body if (!httpResult.IsChunk) { bteReceive = readBytesBatch(stream, httpResult.ResultLength); if (httpResult.IsGzip) { try { bteReceive = JsonHandle.DllLoader.Decompress(bteReceive); } catch { } } httpResult.Body = encoder.GetString(bteReceive, 0, bteReceive.Length); return(httpResult); } Int32 chunkLength = getChunkLength(stream, encoder); MemoryStream memoryStream = new MemoryStream(); while (chunkLength > 0) { try { bteReceive = readBytesBatch(stream, chunkLength); if (bteReceive == null) { break; } memoryStream.Write(bteReceive, 0, chunkLength); } catch { chunkLength = 0; } finally { if (chunkLength > 0) { chunkLength = getChunkLength(stream, encoder); } } } memoryStream.Seek(0, SeekOrigin.Begin); bteReceive = new byte[memoryStream.Length]; memoryStream.Read(bteReceive, 0, bteReceive.Length); memoryStream.Close(); if (httpResult.IsGzip) { try { bteReceive = JsonHandle.DllLoader.Decompress(bteReceive); } catch { } } httpResult.Body = encoder.GetString(bteReceive, 0, bteReceive.Length); return(httpResult); } return(httpResult); } catch { return(null); } finally { try { stream.Close(); } catch { } } }
/// <summary> /// 创建块(携带首片数据),同时检查CRC32 /// </summary> /// <param name="resumeBlockerObj">创建分片上次的块请求</param> private async Task MakeBlock(object resumeBlockerObj) { var resumeBlocker = (ResumeBlocker)resumeBlockerObj; var doneEvent = resumeBlocker.DoneEvent; var blockMakeResults = resumeBlocker.BlockMakeResults; var putExtra = resumeBlocker.PutExtra; var blockIndex = resumeBlocker.BlockIndex; var result = new HttpResult(); //check whether to cancel while (true) { var upCtl = resumeBlocker.PutExtra.UploadController(); if (upCtl == UploadControllerAction.Suspended) { doneEvent.WaitOne(1000); } else if (upCtl == UploadControllerAction.Aborted) { doneEvent.Set(); result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] [ResumableUpload] Info: upload task is aborted, mkblk {blockIndex}\n"; blockMakeResults.Add(blockIndex, result); return; } else { break; } } var blockBuffer = resumeBlocker.BlockBuffer; var blockSize = blockBuffer.Length; var upToken = resumeBlocker.UploadToken; var uploadedBytesDict = resumeBlocker.UploadedBytesDict; var fileSize = resumeBlocker.FileSize; var progressLock = resumeBlocker.ProgressLock; var resumeInfo = resumeBlocker.ResumeInfo; try { //get upload host var ak = UpToken.GetAccessKeyFromUpToken(upToken); var bucket = UpToken.GetBucketFromUpToken(upToken); if (ak == null || bucket == null) { result = HttpResult.InvalidToken; doneEvent.Set(); return; } var uploadHost = await _config.UpHost(ak, bucket); var url = $"{uploadHost}/mkblk/{blockSize}"; var upTokenStr = $"UpToken {upToken}"; using (var ms = new MemoryStream(blockBuffer, 0, blockSize)) { var data = ms.ToArray(); result = await _httpManager.PostDataAsync(url, data, token : upTokenStr); if (result.Code == (int)HttpCode.OK) { var rc = JsonConvert.DeserializeObject <ResumeContext>(result.Text); if (rc.Crc32 > 0) { var crc1 = rc.Crc32; var crc2 = Crc32.CheckSumSlice(blockBuffer, 0, blockSize); if (crc1 != crc2) { result.RefCode = (int)HttpCode.USER_NEED_RETRY; result.RefText += $" CRC32: remote={crc1}, local={crc2}\n"; } else { //write the mkblk context resumeInfo.Contexts[blockIndex] = rc.Ctx; resumeInfo.ExpiredAt = rc.ExpiredAt; lock (progressLock) { uploadedBytesDict["UploadProgress"] += blockSize; } putExtra.ProgressHandler(uploadedBytesDict["UploadProgress"], fileSize); } } else { result.RefText += $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] JSON Decode Error: text = {result.Text}"; result.RefCode = (int)HttpCode.USER_NEED_RETRY; } } else { result.RefCode = (int)HttpCode.USER_NEED_RETRY; } } } catch (Exception ex) { var sb = new StringBuilder(); sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] mkblk Error: "); var e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException qex) { result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } //return the http result blockMakeResults.Add(blockIndex, result); doneEvent.Set(); }
public object Post(Auth request) { AssertAuthProviders(); if (ValidateFn != null) { var validationResponse = ValidateFn(this, HttpMethods.Get, request); if (validationResponse != null) { return(validationResponse); } } if (request.RememberMe.HasValue) { var opt = request.RememberMe.GetValueOrDefault(false) ? SessionOptions.Permanent : SessionOptions.Temporary; base.RequestContext.Get <IHttpResponse>() .AddSessionOptions(base.RequestContext.Get <IHttpRequest>(), opt); } var provider = request.provider ?? AuthProviders[0].Provider; var oAuthConfig = GetAuthProvider(provider); if (oAuthConfig == null) { throw HttpError.NotFound("No configuration was added for OAuth provider '{0}'".Fmt(provider)); } if (request.provider == LogoutAction) { return(oAuthConfig.Logout(this, request)); } var session = this.GetSession(); var referrerUrl = request.Continue ?? session.ReferrerUrl ?? this.RequestContext.GetHeader("Referer") ?? oAuthConfig.CallbackUrl; var isHtml = base.RequestContext.ResponseContentType.MatchesContentType(ContentType.Html); try { var response = Authenticate(request, provider, session, oAuthConfig); var alreadyAuthenticated = response == null; response = response ?? new AuthResponse { UserName = session.UserName, SessionId = session.Id, ReferrerUrl = referrerUrl, }; if (isHtml) { if (alreadyAuthenticated) { return(this.Redirect(referrerUrl.AddHashParam("s", "0"))); } if (!(response is IHttpResult)) { return(new HttpResult(response) { Location = referrerUrl }); } } return(response); } catch (HttpError ex) { referrerUrl = this.RequestContext.GetHeader("Referer"); if (isHtml && referrerUrl != null) { referrerUrl = referrerUrl.SetQueryParam("error", ex.Message); return(HttpResult.Redirect(referrerUrl)); } throw; } }
/// <summary> /// 分片上传/断点续上传,带有自定义进度处理和上传控制,检查CRC32,可自动重试 /// </summary> /// <param name="stream">待上传文件流</param> /// <param name="key">要保存的文件名称</param> /// <param name="upToken">上传凭证</param> /// <param name="putExtra">可选配置参数</param> /// <returns>上传文件后返回结果</returns> public HttpResult UploadStream(Stream stream, string key, string upToken, PutExtra putExtra) { HttpResult result = new HttpResult(); string encodedObjectName = ""; if (putExtra != null && putExtra.Version == "v2") { encodedObjectName = Base64.GetEncodedObjectName(key); } //check put extra if (putExtra == null) { putExtra = new PutExtra(); } if (putExtra.ProgressHandler == null) { putExtra.ProgressHandler = DefaultUploadProgressHandler; } if (putExtra.UploadController == null) { putExtra.UploadController = DefaultUploadController; } if (!(putExtra.BlockUploadThreads > 0 && putExtra.BlockUploadThreads <= 64)) { putExtra.BlockUploadThreads = 1; } using (stream) { //start to upload try { long uploadedBytes = 0; long fileSize = stream.Length; long blockCount = (fileSize + putExtra.PartSize - 1) / putExtra.PartSize; int partNumber = 1; //check resume record file ResumeInfo resumeInfo = null; if (File.Exists(putExtra.ResumeRecordFile)) { resumeInfo = ResumeHelper.Load(putExtra.ResumeRecordFile); if (resumeInfo != null && fileSize == resumeInfo.FileSize) { //check whether ctx expired if (UnixTimestamp.IsContextExpired(resumeInfo.ExpiredAt)) { resumeInfo = null; } } } if (resumeInfo == null) { if (putExtra.Version == "v1") { resumeInfo = new ResumeInfo() { FileSize = fileSize, BlockCount = blockCount, Contexts = new string[blockCount], ExpiredAt = 0, }; } else { HttpResult res = initReq(encodedObjectName, upToken); Dictionary <string, string> responseBody = JsonConvert.DeserializeObject <Dictionary <string, string> >(res.Text); if (res.Code != 200) { return(res); } resumeInfo = new ResumeInfo() { FileSize = fileSize, BlockCount = blockCount, Etags = new Dictionary <string, object> [blockCount], Uploaded = 0, ExpiredAt = long.Parse(responseBody["expireAt"]), UploadId = responseBody["uploadId"] }; } } //calc upload progress for (long blockIndex = 0; blockIndex < blockCount; blockIndex++) { if (putExtra.Version == "v1") { string context = resumeInfo.Contexts[blockIndex]; if (!string.IsNullOrEmpty(context)) { uploadedBytes += putExtra.PartSize; } } else { Dictionary <string, object> etag = resumeInfo.Etags[blockIndex]; if (etag != null) { if (blockIndex > 0) { partNumber += 1; } uploadedBytes += putExtra.PartSize; resumeInfo.Uploaded = uploadedBytes; } } } //set upload progress putExtra.ProgressHandler(uploadedBytes, fileSize); //init block upload error //check not finished blocks to upload UploadControllerAction upCtrl = putExtra.UploadController(); ManualResetEvent manualResetEvent = new ManualResetEvent(false); Dictionary <long, byte[]> blockDataDict = new Dictionary <long, byte[]>(); Dictionary <long, HttpResult> blockMakeResults = new Dictionary <long, HttpResult>(); Dictionary <string, long> uploadedBytesDict = new Dictionary <string, long>(); uploadedBytesDict.Add("UploadProgress", uploadedBytes); byte[] blockBuffer = new byte[putExtra.PartSize]; for (long blockIndex = 0; blockIndex < blockCount; blockIndex++) { string context = null; if (putExtra.Version == "v1") { context = resumeInfo.Contexts[blockIndex]; } else { Dictionary <string, object> etag = resumeInfo.Etags[blockIndex]; if (etag != null && etag.Count > 0) { context = "~"; } } if (string.IsNullOrEmpty(context)) { //check upload controller action before each chunk while (true) { upCtrl = putExtra.UploadController(); if (upCtrl == UploadControllerAction.Aborted) { result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += string.Format("[{0}] [ResumableUpload] Info: upload task is aborted\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); manualResetEvent.Set(); return(result); } else if (upCtrl == UploadControllerAction.Suspended) { result.RefCode = (int)HttpCode.USER_PAUSED; result.RefText += string.Format("[{0}] [ResumableUpload] Info: upload task is paused\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); manualResetEvent.WaitOne(1000); } else if (upCtrl == UploadControllerAction.Activated) { break; } } long offset = blockIndex * putExtra.PartSize; stream.Seek(offset, SeekOrigin.Begin); int blockLen = stream.Read(blockBuffer, 0, putExtra.PartSize); byte[] blockData = new byte[blockLen]; Array.Copy(blockBuffer, blockData, blockLen); blockDataDict.Add(blockIndex, blockData); if (blockDataDict.Count == putExtra.BlockUploadThreads) { processMakeBlocks(blockDataDict, upToken, putExtra, resumeInfo, blockMakeResults, uploadedBytesDict, fileSize, encodedObjectName); //check mkblk results foreach (int blkIndex in blockMakeResults.Keys) { HttpResult mkblkRet = blockMakeResults[blkIndex]; if (mkblkRet.Code != 200) { result = mkblkRet; manualResetEvent.Set(); return(result); } } blockDataDict.Clear(); blockMakeResults.Clear(); if (!string.IsNullOrEmpty(putExtra.ResumeRecordFile)) { ResumeHelper.Save(resumeInfo, putExtra.ResumeRecordFile); } } } } if (blockDataDict.Count > 0) { processMakeBlocks(blockDataDict, upToken, putExtra, resumeInfo, blockMakeResults, uploadedBytesDict, fileSize, encodedObjectName); //check mkblk results foreach (int blkIndex in blockMakeResults.Keys) { HttpResult mkblkRet = blockMakeResults[blkIndex]; if (mkblkRet.Code != 200) { result = mkblkRet; manualResetEvent.Set(); return(result); } } blockDataDict.Clear(); blockMakeResults.Clear(); if (!string.IsNullOrEmpty(putExtra.ResumeRecordFile)) { ResumeHelper.Save(resumeInfo, putExtra.ResumeRecordFile); } } if (upCtrl == UploadControllerAction.Activated) { HttpResult hr = new HttpResult();; if (putExtra.Version == "v1") { hr = MakeFile(key, fileSize, key, upToken, putExtra, resumeInfo.Contexts); } else { hr = completeParts(key, resumeInfo, key, upToken, putExtra, encodedObjectName); } if (hr.Code != (int)HttpCode.OK) { result.Shadow(hr); result.RefText += string.Format("[{0}] [ResumableUpload] Error: mkfile: code = {1}, text = {2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), hr.Code, hr.Text); } if (File.Exists(putExtra.ResumeRecordFile)) { File.Delete(putExtra.ResumeRecordFile); } result.Shadow(hr); result.RefText += string.Format("[{0}] [ResumableUpload] Uploaded: \"{1}\" ==> \"{2}\"\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), putExtra.ResumeRecordFile, key); } else { result.Code = (int)HttpCode.USER_CANCELED; result.RefCode = (int)HttpCode.USER_CANCELED; result.RefText += string.Format("[{0}] [ResumableUpload] Info: upload task is aborted, mkfile\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); } manualResetEvent.Set(); return(result); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}] [ResumableUpload] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); Exception e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } return(result); }
public async Task <IActionResult> Get([FromQuery] RegisterFilter filters) { var result = new HttpResult <dynamic>(this._logger); try { if (filters.FilterBehavior == FilterBehavior.GetDataItem) { var searchResult = await this._rep.GetDataItem(filters); return(result.ReturnCustomResponse(searchResult, filters)); } if (!this._user.GetClaims().GetTools().VerifyClaimsCanRead("Register")) { return(new ObjectResult(null) { StatusCode = (int)HttpStatusCode.Forbidden }); } if (filters.FilterBehavior == FilterBehavior.GetDataCustom) { var searchResult = await this._rep.GetDataCustom(filters); return(result.ReturnCustomResponse(searchResult, filters)); } if (filters.FilterBehavior == FilterBehavior.GetDataListCustom) { var searchResult = await this._rep.GetDataListCustom(filters); return(result.ReturnCustomResponse(searchResult, filters)); } if (filters.FilterBehavior == FilterBehavior.GetDataListCustomPaging) { var paginatedResult = await this._rep.GetDataListCustomPaging(filters); return(result.ReturnCustomResponse(paginatedResult.ToSearchResult <dynamic>(), filters)); } if (filters.FilterBehavior == FilterBehavior.Export) { var searchResult = await this._rep.GetDataListCustom(filters); var export = new ExportExcelCustom <dynamic>(filters); var file = export.ExportFile(this.Response, searchResult, "Register", this._env.RootPath); return(File(file, export.ContentTypeExcel(), export.GetFileName())); } throw new InvalidOperationException("invalid FilterBehavior"); } catch (Exception ex) { var responseEx = result.ReturnCustomException(ex, "Seed - Register", filters, new ErrorMapCustom()); return(responseEx); } }
/// <summary> /// 根据已上传的所有分片数据创建文件 /// </summary> /// <param name="fileName">源文件名</param> /// <param name="resumeInfo">分片上传记录信息</param> /// <param name="key">要保存的文件名</param> /// <param name="upToken">上传凭证</param> /// <param name="putExtra">用户指定的额外参数</param> /// <param name="encodedObjectName">Base64编码后的资源名</param> /// <returns>此操作执行后的返回结果</returns> private HttpResult completeParts(string fileName, ResumeInfo resumeInfo, string key, string upToken, PutExtra putExtra, string encodedObjectName) { HttpResult result = new HttpResult(); try { string paramStr = "{}"; if (string.IsNullOrEmpty(fileName)) { fileName = "fname"; } if (string.IsNullOrEmpty(putExtra.MimeType)) { putExtra.MimeType = ""; } if (string.IsNullOrEmpty(key)) { key = ""; } if (putExtra.Params != null) { paramStr = JsonConvert.SerializeObject(putExtra.Params); } //get upload host string ak = UpToken.GetAccessKeyFromUpToken(upToken); string bucket = UpToken.GetBucketFromUpToken(upToken); if (ak == null || bucket == null) { return(HttpResult.InvalidToken); } string uploadHost = this.config.UpHost(ak, bucket); string upTokenStr = string.Format("UpToken {0}", upToken); Dictionary <string, object> body = new Dictionary <string, object>(); body.Add("fname", fileName); body.Add("mimeType", putExtra.MimeType); body.Add("customVars", null); body.Add("parts", resumeInfo.Etags); string url = string.Format("{0}/buckets/{1}/objects/{2}/uploads/{3}", uploadHost, bucket, encodedObjectName, resumeInfo.UploadId); string bodyStr = JsonConvert.SerializeObject(body); result = httpManager.PostJson(url, bodyStr, upTokenStr); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}] completeParts Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); Exception e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException) { QiniuException qex = (QiniuException)ex; result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } return(result); }
public async Task GetExternalLogins() { HttpResult <ExternalLogin[]> result = await client.GetExternalLoginsAsync("Todo.WindowsStore"); Assert.IsNotNull(result.Content); }
/// <summary> /// 根据已上传的所有分片数据创建文件 /// </summary> /// <param name="fileName">源文件名</param> /// <param name="size">文件大小</param> /// <param name="key">要保存的文件名</param> /// <param name="contexts">所有数据块的Context</param> /// <param name="upToken">上传凭证</param> /// <param name="putExtra">用户指定的额外参数</param> /// <returns>此操作执行后的返回结果</returns> private HttpResult MakeFile(string fileName, long size, string key, string upToken, PutExtra putExtra, string[] contexts) { HttpResult result = new HttpResult(); try { string fnameStr = "fname"; string mimeTypeStr = ""; string keyStr = ""; string paramStr = ""; //check file name if (!string.IsNullOrEmpty(fileName)) { fnameStr = string.Format("/fname/{0}", Base64.UrlSafeBase64Encode(fileName)); } //check mime type if (!string.IsNullOrEmpty(putExtra.MimeType)) { mimeTypeStr = string.Format("/mimeType/{0}", Base64.UrlSafeBase64Encode(putExtra.MimeType)); } //check key if (!string.IsNullOrEmpty(key)) { keyStr = string.Format("/key/{0}", Base64.UrlSafeBase64Encode(key)); } //check extra params if (putExtra.Params != null && putExtra.Params.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (var kvp in putExtra.Params) { string k = kvp.Key; string v = kvp.Value; if (k.StartsWith("x:") && !string.IsNullOrEmpty(v)) { sb.AppendFormat("/{0}/{1}", k, v); } } paramStr = sb.ToString(); } //get upload host string ak = UpToken.GetAccessKeyFromUpToken(upToken); string bucket = UpToken.GetBucketFromUpToken(upToken); if (ak == null || bucket == null) { return(HttpResult.InvalidToken); } string uploadHost = this.config.UpHost(ak, bucket); string url = string.Format("{0}/mkfile/{1}{2}{3}{4}{5}", uploadHost, size, mimeTypeStr, fnameStr, keyStr, paramStr); string body = string.Join(",", contexts); string upTokenStr = string.Format("UpToken {0}", upToken); result = httpManager.PostText(url, body, upTokenStr); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}] mkfile Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")); Exception e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); if (ex is QiniuException) { QiniuException qex = (QiniuException)ex; result.Code = qex.HttpResult.Code; result.RefCode = qex.HttpResult.Code; result.Text = qex.HttpResult.Text; result.RefText += sb.ToString(); } else { result.RefCode = (int)HttpCode.USER_UNDEF; result.RefText += sb.ToString(); } } return(result); }
/// <summary> /// 从uc.qbox.me查询得到回复后,解析出upHost,然后根据upHost确定Zone /// </summary> /// <param name="accessKey">AccessKek</param> /// <param name="bucket">空间名称</param> public async Task <Zone> QueryZone(string accessKey, string bucket) { Zone zone = null; var cacheKey = $"{accessKey}:{bucket}"; //check from cache lock (RwLock) { if (ZoneCache.ContainsKey(cacheKey)) { zone = ZoneCache[cacheKey]; } } if (zone != null) { return(zone); } //query from uc api HttpResult hr = null; try { var queryUrl = $"https://uc.qbox.me/v2/query?ak={accessKey}&bucket={bucket}"; hr = await _httpManager.GetAsync(queryUrl); if (hr.Code == (int)HttpCode.OK) { var zInfo = JsonConvert.DeserializeObject <ZoneInfo>(hr.Text); if (zInfo != null) { zone = new Zone { SrcUpHosts = zInfo.Up.Src.Main, CdnUpHosts = zInfo.Up.Acc.Main, IovipHost = zInfo.Io.Src.Main[0] }; if (zone.IovipHost.Contains("z1")) { zone.ApiHost = "api-z1.qiniu.com"; zone.RsHost = "rs-z1.qiniu.com"; zone.RsfHost = "rsf-z1.qiniu.com"; } else if (zone.IovipHost.Contains("z2")) { zone.ApiHost = "api-z2.qiniu.com"; zone.RsHost = "rs-z2.qiniu.com"; zone.RsfHost = "rsf-z2.qiniu.com"; } else if (zone.IovipHost.Contains("na0")) { zone.ApiHost = "api-na0.qiniu.com"; zone.RsHost = "rs-na0.qiniu.com"; zone.RsfHost = "rsf-na0.qiniu.com"; } else if (zone.IovipHost.Contains("as0")) { zone.ApiHost = "api-as0.qiniu.com"; zone.RsHost = "rs-as0.qiniu.com"; zone.RsfHost = "rsf-as0.qiniu.com"; } else { zone.ApiHost = "api.qiniu.com"; zone.RsHost = "rs.qiniu.com"; zone.RsfHost = "rsf.qiniu.com"; } lock (RwLock) { ZoneCache[cacheKey] = zone; } } else { throw new Exception("JSON Deserialize failed: " + hr.Text); } } else { throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText); } } catch (Exception ex) { var sb = new StringBuilder(); sb.Append($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}] QueryZone Error: "); var e = ex; while (e != null) { sb.Append(e.Message + " "); e = e.InnerException; } sb.AppendLine(); throw new QiniuException(hr, sb.ToString()); } return(zone); }
public async Task Can_seek_from_middle_to_middle() { var mockRequest = new MockHttpRequest(); mockRequest.Headers.Add("Range", "bytes=3-5"); var mockResponse = new MockHttpResponse(mockRequest); string customText = "1234567890"; byte[] customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, "audio/mpeg"); bool reponseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.ReadAsString(); Assert.That(writtenString, Is.EqualTo("456")); Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 3-5/10")); Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString())); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(206)); }
public async Task Can_use_fileStream() { byte[] fileBytes = uploadedTextFile.ReadFully(); string fileText = Encoding.ASCII.GetString(fileBytes); "File content size {0}".Print(fileBytes.Length); "File content is {0}".Print(fileText); var mockRequest = new MockHttpRequest(); var mockResponse = new MockHttpResponse(mockRequest); mockRequest.Headers.Add("Range", "bytes=6-8"); var httpResult = new HttpResult(uploadedTextFile, "audio/mpeg"); bool reponseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.ReadAsString(); Assert.That(writtenString, Is.EqualTo(fileText.Substring(6, 3))); Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 6-8/33")); Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString())); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(206)); }
public static bool UploadFileData(string filename, byte[] data, string filepath) { // // 上传文件名 string key = filepath + filename; // // 本地文件路径 // string filePath = filepath; // // 存储空间名 // string Bucket = senparcQiniuSetting.QiniuOSSPictureBucket; // // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policytouchun // PutPolicy putPolicy = new PutPolicy(); // putPolicy.Scope = Bucket; //// putPolicy.Scope = Bucket; // putPolicy.SetExpires(3600); // //putPolicy.DeleteAfterDays = 1; // string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); // Config config = new Config(); // // 设置上传区域 // Qiniu.Common.Config.AutoZone(senparcQiniuSetting.QiniuAccessKeyId, Bucket, true); // Qiniu.CDN.CdnManager cdn = new Qiniu.CDN.CdnManager(mac); // // 表单上传 // FormUploader target = new FormUploader(true); // //HttpResult result = target.UploadData(data, key, token, null); // HttpResult result = target.UploadData(data, key, token); // if (result != null && result.Code == 200) // { // return true; // } // else // { // return false; // } // // HttpResult result = target.UploadFile(filePath, key, token, null); // Console.WriteLine("form upload result: " + result.ToString()); // 生成(上传)凭证时需要使用此Mac // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey // 实际应用中,请自行设置您的AccessKey和SecretKey Mac mac = new Mac(senparcQiniuSetting.QiniuAccessKeyId, senparcQiniuSetting.QiniuOSSAccessKeySecret); //string bucket = "test"; //string saveKey = "myfile"; // byte[] data = System.IO.File.ReadAllBytes("D:/QFL/1.mp3"); //byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!"); // 上传策略,参见 // https://developer.qiniu.com/kodo/manual/put-policy PutPolicy putPolicy = new PutPolicy(); // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY" // putPolicy.Scope = bucket + ":" + saveKey; putPolicy.Scope = senparcQiniuSetting.QiniuOSSPictureBucket; Qiniu.Common.Config.AutoZone(senparcQiniuSetting.QiniuAccessKeyId, senparcQiniuSetting.QiniuOSSPictureBucket, true); // 上传策略有效期(对应于生成的凭证的有效期) putPolicy.SetExpires(3600); // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除 // putPolicy.DeleteAfterDays = 1; // 生成上传凭证,参见 // https://developer.qiniu.com/kodo/manual/upload-token string jstr = putPolicy.ToJsonString(); string token = Auth.CreateUploadToken(mac, jstr); FormUploader fu = new FormUploader(); HttpResult result = fu.UploadData(data, key, token); if (result != null && result.Code == 200) { return(true); } else { return(false); } }
public object Get(PartialFromText request) { const string customText = "123456789012345678901234567890"; var httpResult = new HttpResult(customText, "text/plain"); return httpResult; }
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) { var tokens = this.Init(authService, ref session, request); //Transfering AccessToken/Secret from Mobile/Desktop App to Server if (request?.AccessToken != null) { if (VerifyAccessToken == null) { throw new NotImplementedException($"VerifyAccessToken is not implemented by {Provider}"); } if (!VerifyAccessToken(request.AccessToken)) { return(HttpError.Unauthorized($"AccessToken is not for the configured {Provider} App")); } var failedResult = AuthenticateWithAccessToken(authService, session, tokens, request.AccessToken); var isHtml = authService.Request.IsHtml(); if (failedResult != null) { return(ConvertToClientError(failedResult, isHtml)); } return(isHtml ? authService.Redirect(SuccessRedirectUrlFilter(this, session.ReferrerUrl.SetParam("s", "1"))) : null); //return default AuthenticateResponse } var authServer = new AuthorizationServerDescription { AuthorizationEndpoint = new Uri(this.AuthorizeUrl), TokenEndpoint = new Uri(this.AccessTokenUrl) }; AuthServerFilter?.Invoke(authServer); var authClient = new WebServerClient(authServer, this.ConsumerKey) { ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(this.ConsumerSecret), }; AuthClientFilter?.Invoke(authClient); var authState = ProcessUserAuthorization(authClient, authServer, authService); if (authState == null) { try { var authReq = authClient.PrepareRequestUserAuthorization(this.Scopes, new Uri(this.CallbackUrl)); var authContentType = authReq.Headers[HttpHeaders.ContentType]; var httpResult = new HttpResult(authReq.ResponseStream, authContentType) { StatusCode = authReq.Status, StatusDescription = "Moved Temporarily" }; foreach (string header in authReq.Headers) { httpResult.Headers[header] = authReq.Headers[header]; } foreach (string name in authReq.Cookies) { var cookie = authReq.Cookies[name]; if (cookie != null) { httpResult.Cookies.Add(cookie.ToCookie()); } } this.SaveSession(authService, session, SessionExpiry); return(httpResult); } catch (ProtocolException ex) { Log.Error("Failed to login to {0}".Fmt(this.Provider), ex); return(authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", "Unknown")))); } } var accessToken = authState.AccessToken; if (accessToken != null) { tokens.RefreshToken = authState.RefreshToken; tokens.RefreshTokenExpiry = authState.AccessTokenExpirationUtc; } if (accessToken != null) { try { return(AuthenticateWithAccessToken(authService, session, tokens, accessToken) ?? authService.Redirect(SuccessRedirectUrlFilter(this, session.ReferrerUrl.SetParam("s", "1")))); } catch (WebException we) { var statusCode = ((HttpWebResponse)we.Response).StatusCode; if (statusCode == HttpStatusCode.BadRequest) { return(authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", "AccessTokenFailed")))); } } } return(authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", "RequestTokenFailed")))); }