当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.SetCopyleaksClient方法代码示例

本文整理汇总了C#中System.Net.Http.HttpClient.SetCopyleaksClient方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetCopyleaksClient方法的具体用法?C# HttpClient.SetCopyleaksClient怎么用?C# HttpClient.SetCopyleaksClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpClient的用法示例。


在下文中一共展示了HttpClient.SetCopyleaksClient方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateProcessAsync

		/// <summary>
		/// 
		/// </summary>
		/// <param name="url"></param>
		/// <exception cref="UnauthorizedAccessException"></exception>
		/// <returns></returns>
		public async Task<ScannerProcess> CreateProcessAsync(string url)
		{
			if (this.Token == null)
				throw new UnauthorizedAccessException("Empty token!");
			else
				this.Token.Validate();

			using (HttpClient client = new HttpClient())
			{
				client.SetCopyleaksClient(HttpContentTypes.Json, this.Token);

				CreateCommandRequest req = new CreateCommandRequest() { URL = url };

				HttpResponseMessage msg;
				if (File.Exists(url))
				{
					FileInfo localFile = new FileInfo(url);
					// Local file. Need to upload it to the server.

					using (var content = new MultipartFormDataContent("Upload----" + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)))
					{
						using (FileStream stream = File.OpenRead(url))
						{
							content.Add(new StreamContent(stream, (int)stream.Length), "document", Path.GetFileName(url));
							msg = client.PostAsync(Resources.ServiceVersion + "/detector/create-by-file", content).Result;
						}
					}
				}
				else
				{
					// Internet path. Just submit it to the server.
					HttpContent content = new StringContent(
						JsonConvert.SerializeObject(req),
						Encoding.UTF8,
						HttpContentTypes.Json);
					msg = client.PostAsync(Resources.ServiceVersion + "/detector/create-by-url", content).Result;
				}

				if (!msg.IsSuccessStatusCode)
				{
					var errorJson = await msg.Content.ReadAsStringAsync();
					var errorObj = JsonConvert.DeserializeObject<BadResponse>(errorJson);
					if (errorObj == null)
						throw new CommandFailedException(msg.StatusCode);
					else
						throw new CommandFailedException(errorObj.Message, msg.StatusCode);
				}

				string json = await msg.Content.ReadAsStringAsync();

				CreateResourceResponse response = JsonConvert.DeserializeObject<CreateResourceResponse>(json);
				return new ScannerProcess(this.Token, response.ProcessId);
			}
		}
开发者ID:eladbitton,项目名称:.NET-API,代码行数:60,代码来源:Detector.cs

示例2: Login

		/// <summary>
		/// Log-in into copyleaks authentication system.
		/// </summary>
		/// <param name="username">User name</param>
		/// <param name="apiKey">Password</param>
		/// <returns>Login Token to use while accessing resources.</returns>
		/// <exception cref="ArgumentException">Occur when the username and\or password is empty</exception>
		/// <exception cref="JsonException">ALON</exception>
		public static LoginToken Login(string username, string apiKey)
		{
#if !DEBUG // For testing purposes
			if (string.IsNullOrEmpty(username))
				throw new ArgumentException("Username is mandatory.", "username");
			else if (string.IsNullOrEmpty(apiKey))
				throw new ArgumentException("Password is mandatory.", "password");
#endif

			LoginToken token;
			using (HttpClient client = new HttpClient())
			{
				client.SetCopyleaksClient(HttpContentTypes.UrlEncoded);
				HttpResponseMessage msg = client.PostAsync(LOGIN_PAGE, new FormUrlEncodedContent(new[]
					{
						new KeyValuePair<string, string>("username", username),
						new KeyValuePair<string, string>("apikey", apiKey)
					})).Result;

				if (!msg.IsSuccessStatusCode)
				{
					string errorResponse = msg.Content.ReadAsStringAsync().Result;
					BadLoginResponse response = JsonConvert.DeserializeObject<BadLoginResponse>(errorResponse);
					if (response == null)
						throw new JsonException("Unable to process server response.");
					else
						throw new CommandFailedException(response.Message, msg.StatusCode);
				}

				string json = msg.Content.ReadAsStringAsync().Result;

				if (string.IsNullOrEmpty(json))
					throw new JsonException("This request could not be processed.");

				token = JsonConvert.DeserializeObject<LoginToken>(json);
				if (token == null)
					throw new JsonException("Unable to process server response.");
			}

			return token;
		}
开发者ID:eladbitton,项目名称:.NET-API,代码行数:49,代码来源:UsersAuthentication.cs

示例3: CreateByUrl

		/// <summary>
		/// 
		/// </summary>
		/// <param name="url">The url of the content to scan</param>
		/// <exception cref="UnauthorizedAccessException">When security-token is undefined or expired</exception>
		/// <exception cref="ArgumentOutOfRangeException">When the input url schema is diffrent then HTTP and HTTPS</exception>
		/// <returns></returns>
		public ScannerProcess CreateByUrl(Uri url, Uri httpCallback = null)
		{
			if (this.Token == null)
				throw new UnauthorizedAccessException("Empty token!");
			else
				this.Token.Validate();

			if (url.Scheme != "http" && url.Scheme != "https")
				throw new ArgumentOutOfRangeException(nameof(url), "Allowed protocols: HTTP, HTTPS");

			using (HttpClient client = new HttpClient())
			{
				client.SetCopyleaksClient(HttpContentTypes.Json, this.Token);

				CreateCommandRequest req = new CreateCommandRequest() { URL = url.AbsoluteUri };

				HttpResponseMessage msg;
				// Internet path. Just submit it to the server.
				HttpContent content = new StringContent(
					JsonConvert.SerializeObject(req),
					Encoding.UTF8,
					HttpContentTypes.Json);

				if (httpCallback != null)
					client.DefaultRequestHeaders.Add("Http-Callback", httpCallback.AbsoluteUri); // Add HTTP callback to the request header.

				msg = client.PostAsync(Resources.ServiceVersion + "/detector/create-by-url", content).Result;

				if (!msg.IsSuccessStatusCode)
				{
					var errorJson = msg.Content.ReadAsStringAsync().Result;
					var errorObj = JsonConvert.DeserializeObject<BadResponse>(errorJson);
					if (errorObj == null)
						throw new CommandFailedException(msg.StatusCode);
					else
						throw new CommandFailedException(errorObj.Message, msg.StatusCode);
				}

				string json = msg.Content.ReadAsStringAsync().Result;

				CreateResourceResponse response = JsonConvert.DeserializeObject<CreateResourceResponse>(json);
				return new ScannerProcess(this.Token, response.ProcessId);
			}
		}
开发者ID:eladbitton,项目名称:.NET-API,代码行数:51,代码来源:Detector.cs

示例4: CreateByOcr

		/// <summary>
		/// 
		/// </summary>
		/// <param name="localfile"></param>
		/// <exception cref="UnauthorizedAccessException"></exception>
		/// <returns></returns>
		public ScannerProcess CreateByOcr(FileInfo localfile, Uri httpCallback = null)
		{
			if (this.Token == null)
				throw new UnauthorizedAccessException("Empty token!");
			else
				this.Token.Validate();

			if (!localfile.Exists)
				throw new FileNotFoundException("File not found!", localfile.FullName);

			using (HttpClient client = new HttpClient())
			{
				client.SetCopyleaksClient(HttpContentTypes.Json, this.Token);

				CreateCommandRequest req = new CreateCommandRequest()
				{
					URL = localfile.FullName
				};

				if (httpCallback != null)
					client.DefaultRequestHeaders.Add("Http-Callback", httpCallback.AbsoluteUri); // Add HTTP callback to the request header.

				HttpResponseMessage msg;
				// Local file. Need to upload it to the server.

				using (var content = new MultipartFormDataContent("Upload----" + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)))
				using (FileStream stream = localfile.OpenRead())
				{
					content.Add(new StreamContent(stream, (int)stream.Length), "document", Path.GetFileName(localfile.Name));
					msg = client.PostAsync(Resources.ServiceVersion + "/detector/create-by-file-ocr", content).Result;
				}

				if (!msg.IsSuccessStatusCode)
				{
					var errorJson = msg.Content.ReadAsStringAsync().Result;
					var errorObj = JsonConvert.DeserializeObject<BadResponse>(errorJson);
					if (errorObj == null)
						throw new CommandFailedException(msg.StatusCode);
					else
						throw new CommandFailedException(errorObj.Message, msg.StatusCode);
				}

				string json = msg.Content.ReadAsStringAsync().Result;

				CreateResourceResponse response = JsonConvert.DeserializeObject<CreateResourceResponse>(json);
				return new ScannerProcess(this.Token, response.ProcessId);
			}
		}
开发者ID:eladbitton,项目名称:.NET-API,代码行数:54,代码来源:Detector.cs

示例5: CountCreditsAsync

		public static async Task<uint> CountCreditsAsync(LoginToken token)
		{
			token.Validate();

			if (token == null)
				throw new ArgumentNullException("token");

			using (HttpClient client = new HttpClient())
			{
				client.SetCopyleaksClient(HttpContentTypes.Json, token);

				HttpResponseMessage msg = await client.GetAsync(string.Format("{0}/account/count-credits", Resources.ServiceVersion));
				if (!msg.IsSuccessStatusCode)
				{
					string errorResponse = await msg.Content.ReadAsStringAsync();
					BadLoginResponse response = JsonConvert.DeserializeObject<BadLoginResponse>(errorResponse);
					if (response == null)
						throw new JsonException("Unable to process server response.");
					else
						throw new CommandFailedException(response.Message, msg.StatusCode);
				}

				string json = await msg.Content.ReadAsStringAsync();

				if (string.IsNullOrEmpty(json))
					throw new JsonException("This request could not be processed.");

				CountCreditsResponse res = JsonConvert.DeserializeObject<CountCreditsResponse>(json);
				if (token == null)
					throw new JsonException("Unable to process server response.");

				return res.Amount;
			}
		}
开发者ID:eladbitton,项目名称:.NET-API,代码行数:34,代码来源:UsersAuthentication.cs


注:本文中的System.Net.Http.HttpClient.SetCopyleaksClient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。