Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Web;
- using System.IO;
- using System.Threading.Tasks;
- namespace WebTestHttpClient.Controllers
- {
- public class ValuesController : ApiController
- {
- // GET api/values
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- private string ПолучитьЗначения(IEnumerable<string> values)
- {
- var sb = new StringBuilder();
- foreach (var h in values)
- {
- sb.Append(h+",");
- }
- if (sb.Length == 0) return "";
- return sb.ToString(0, sb.Length - 1);
- }
- [HttpGet]
- public async Task<string> GetId()
- {
- await Task.Delay(1000);
- return "1";
- }
- [HttpGet]
- public async Task<string> GetIdAsync(string id)
- {
- await Task.Delay(1000);
- return id;
- }
- void ЗаполнитьКукиИХэдеры(StringBuilder sb)
- {
- sb.AppendLine("======= Куки ");
- foreach (var cookies in Request.Headers.GetCookies())
- {
- foreach (var cooki in cookies.Cookies)
- sb.AppendLine(cooki.Name + ":" + cooki.Value);
- }
- sb.AppendLine("======= Заголовки ");
- foreach (var h in Request.Headers)
- {
- sb.AppendLine(h.Key + ":" + ПолучитьЗначения(h.Value));
- }
- }
- // GET api/values/5
- [HttpGet]
- public HttpResponseMessage GetHeaders()
- {
- var cookie = new CookieHeaderValue("id", "12345"); // имя куки - id, значение - 12345
- cookie.Expires = DateTimeOffset.Now.AddDays(1); // время действия куки - 1 день
- cookie.Domain = Request.RequestUri.Host; // домен куки
- cookie.Path = "/"; // путь куки
- var sb = new StringBuilder();
- ЗаполнитьКукиИХэдеры(sb);
- var response = Request.CreateResponse<string>(HttpStatusCode.OK, sb.ToString());
- response.Headers.AddCookies(new CookieHeaderValue[] { cookie });
- return response;
- }
- // POST api/values
- [HttpPost]
- public string SendStr([FromBody]string value)
- {
- string Id = "";
- CookieHeaderValue cookie = Request.Headers.GetCookies("id").FirstOrDefault();
- if (cookie != null)
- {
- Id = cookie["id"].Value;
- }
- var sb = new StringBuilder();
- sb.AppendLine("value=" + value);
- sb.AppendLine("id=" + Id);
- ЗаполнитьКукиИХэдеры(sb);
- return sb.ToString();
- }
- [HttpPost]
- public string SendStr2()
- {
- string Id = "";
- CookieHeaderValue cookie = Request.Headers.GetCookies("id").FirstOrDefault();
- if (cookie != null)
- {
- Id = cookie["id"].Value;
- }
- var sb = new StringBuilder();
- string value = Request.Content.ReadAsStringAsync().Result;
- sb.AppendLine("value=" + value);
- sb.AppendLine("id=" + Id);
- ЗаполнитьКукиИХэдеры(sb);
- return sb.ToString();
- }
- [HttpPost]
- public string SendStream()
- {
- string Id = "";
- CookieHeaderValue cookie = Request.Headers.GetCookies("id").FirstOrDefault();
- if (cookie != null)
- {
- Id = cookie["id"].Value;
- }
- var sb = new StringBuilder();
- string value = Request.Content.ReadAsStringAsync().Result;
- sb.AppendLine("value=" + value);
- sb.AppendLine("id=" + Id);
- foreach (var h in Request.Headers)
- {
- sb.AppendLine(h.Key + ":" + ПолучитьЗначения(h.Value));
- }
- return sb.ToString();
- }
- [NonAction]
- string fileName(MultipartFileData fileData)
- {
- string fileName = fileData.Headers.ContentDisposition.FileName;
- if (string.IsNullOrEmpty(fileName))
- {
- return "";
- }
- if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
- {
- fileName = fileName.Trim('"');
- }
- if (fileName.Contains(@"/") || fileName.Contains(@"\"))
- {
- fileName = Path.GetFileName(fileName);
- }
- return fileName;
- }
- [HttpPost]
- public async Task<HttpResponseMessage> SendFiles()
- {
- if (!Request.Content.IsMimeMultipartContent())
- {
- throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
- }
- string root = HttpContext.Current.Server.MapPath("~/App_Data");
- var provider = new MultipartFormDataStreamProvider(root);
- try
- {
- StringBuilder sb = new StringBuilder(); // Holds the response body
- // Read the form data and return an async task.
- await Request.Content.ReadAsMultipartAsync(provider);
- // This illustrates how to get the form data.
- foreach (var key in provider.FormData.AllKeys)
- {
- foreach (var val in provider.FormData.GetValues(key))
- {
- sb.Append(string.Format("{0}: {1}\n", key, val));
- }
- }
- // This illustrates how to get the file names for uploaded files.
- foreach (var file in provider.FileData)
- {
- var fileInfo = new FileInfo(file.LocalFileName);
- sb.Append(string.Format("Uploaded file: {0} {1} ({2} bytes)\n", fileName(file), fileInfo.Name, fileInfo.Length));
- }
- return new HttpResponseMessage()
- {
- Content = new StringContent(sb.ToString())
- };
- }
- catch (System.Exception e)
- {
- return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
- }
- }
- [HttpPost]
- public string SendUserPassword(WebTestHttpClient.Areas.HelpPage.Models.UserPassword UP)
- {
- if (UP == null) return "null";
- return UP.Login+":"+UP.Password;
- }
- // PUT api/values/5
- public void Put(int id, [FromBody]string value)
- {
- }
- // DELETE api/values/5
- public void Delete(int id)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement