public string PostTxt(string url, object arg, bool encoded) { string result = null; try { CreateRequest(url, "POST"); string body = encoded ? JSON.Instance.ToJSON(arg) : arg.ToString(); byte[] buf = ContentEncoding.GetBytes(body); request.ContentLength = buf.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(buf, 0, buf.Length); } //request WebResponse response = request.GetResponse(); using (StreamReader sr = new StreamReader(response.GetResponseStream(), ContentEncoding)) { result = sr.ReadToEnd(); } } catch (Exception ex) { Debug.WriteLine(ex); } return(result); }
private void RenderRazorView(HttpResponseBase response) { string htmlView = this.RenderViewToString(this.Context, this.ViewName, this.ViewModel); var streambuffer = ContentEncoding.GetBytes(htmlView); response.OutputStream.Write(streambuffer, 0, streambuffer.Length); }
/// <summary> /// 重写ExecuteResult /// </summary> /// <param name="context">ControllerContext</param> public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } HttpResponseBase response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding == null) { ContentEncoding = Encoding.UTF8; } response.ContentEncoding = ContentEncoding; if (Data != null) { var data = NewtonsoftSerialize(Data); byte[] bytes = ContentEncoding.GetBytes(data); response.BufferOutput = true; response.AddHeader("Content-Length", bytes.Length.ToString()); response.BinaryWrite(bytes); } }
public IResponse Write(string text) { // Construct a response. var buffer = ContentEncoding.GetBytes(text); return(Write(buffer)); }
/// <summary> /// Closes the stream and renders its content to the next filter in the chain of filters. /// </summary> public override void Close() { _stream.Seek(0, SeekOrigin.Begin); var responseContent = RenderResponse(); base.Close(); var buffer = ContentEncoding.GetBytes(responseContent); _next.Write(buffer, 0, buffer.Length); }
public virtual object Deserialize(RestResponse response, Type type) { object instance; using (var stream = new MemoryStream(ContentEncoding.GetBytes(response.Content))) { var serializer = CacheOrGetSerializerFor(type); instance = serializer.ReadObject(stream); } return(instance); }
public virtual T Deserialize <T>(RestResponse <T> response) { var type = typeof(T); T instance; using (var stream = new MemoryStream(ContentEncoding.GetBytes(response.Content))) { var serializer = CacheOrGetSerializerFor(type); instance = (T)serializer.ReadObject(stream); } return(instance); }
protected override void WriteFile(HttpResponseBase response) { response.ContentEncoding = this.ContentEncoding; response.BufferOutput = this.BufferOutput; var streambuffer = ContentEncoding.GetBytes(this.GetCSVData()); if (HasPreamble) { var preamble = this.ContentEncoding.GetPreamble(); response.OutputStream.Write(preamble, 0, preamble.Length); } response.OutputStream.Write(streambuffer, 0, streambuffer.Length); }
public void SendResponse(HttpStatusCode statusCode, string response = null) { StatusDescription = statusCode.ToString().ConvertCamelCase(); StatusCode = statusCode; byte[] buffer; if (string.IsNullOrWhiteSpace(response)) { ContentType = ContentType.HTML; buffer = Encoding.ASCII.GetBytes($"<h1>{StatusDescription}</h1>"); } else { buffer = ContentEncoding.GetBytes(response); } FlushResponse(buffer); }
internal void InjectTextParts(string value) { byte[] textParts = ContentEncoding.GetBytes(value); if (_worker.GetType().FullName.StartsWith("Mono.")) { InjectTextParts(_request, textParts); } else if (_worker.GetType().Name == "IIS7WorkerRequest") { InjectTextPartsIIS7(_request, textParts); } else { InjectTextParts(_worker, textParts); InjectTextParts(_request, textParts); } }
public override void Write(byte[] buffer, int offset, int count) { string contentInBuffer = ContentEncoding.GetString(buffer); if (BodyEnd.IsMatch(contentInBuffer)) { string bodyCloseWithScript = BodyEnd.Replace(contentInBuffer, HtmlSnippet); byte[] outputBuffer = ContentEncoding.GetBytes(bodyCloseWithScript); OutputStream.Write(outputBuffer, 0, outputBuffer.Length); } else { Logger.Warn("Unable to locate '</body>' with content encoding '{0}'. Response may be compressed.", ContentEncoding.EncodingName); OutputStream.Write(buffer, offset, count); } }
/// <summary> /// Returns a byte array representation of the data in the file referenced by the stream /// </summary> /// <param name="stream"></param> /// <param name="istext"></param> /// <returns></returns> protected byte[] GetFileBytes(FileStream stream, bool istext) { byte[] buffer; if (istext) { using (var reader = new StreamReader(stream)) { buffer = ContentEncoding.GetBytes(reader.ReadToEnd()); } } else { using (var reader = new BinaryReader(stream)) { buffer = reader.ReadBytes((int)stream.Length); } } return(buffer); }
public byte [] GetBody() { StringBuilder data = null; if (PostBody != null) { data = new StringBuilder(); data.Append(PostBody); } if (post_data != null) { data = new StringBuilder(); bool first = true; foreach (string key in post_data.Keys) { if (!first) { data.Append('&'); } first = false; UnsafeString s = post_data.Get(key); if (s != null) { data.AppendFormat("{0}={1}", key, s.UnsafeValue); continue; } } } if (data == null) { return(null); } return(ContentEncoding.GetBytes(data.ToString())); }
private void Execute(ControllerContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } HttpResponse response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Content != null) { var buffer = ContentEncoding.GetBytes(Content); response.ResponseStream.Write(buffer, 0, buffer.Length); } }
public void Write(string str) { byte [] data = ContentEncoding.GetBytes(str); WriteToBody(data, 0, data.Length); }
private void WriteToOutputStream(string content) { byte[] outputBuffer = ContentEncoding.GetBytes(content); OutputStream.Write(outputBuffer, 0, outputBuffer.Length); }
/// <summary> /// Add a file to index (without relying on the working directory) by specifying the file's content as string. /// The added file doesn't need to exist in the working directory. /// </summary> /// <param name="path">Relative path in the working directory. Note: the path is encoded using PathEncoding</param> /// <param name="content">The content as string. Note: the content is encoded using ContentEncoding</param> public void AddContent(string path, string content) { AddContent(PathEncoding.GetBytes(path), ContentEncoding.GetBytes(content)); }
/// <summary> /// 创建请求 /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <returns></returns> public HttpWebRequest CreateWebRequest(string url, string method, string postData) { Uri URI = new Uri(url); HttpWebRequest request; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; if (!string.IsNullOrEmpty(certFile)) { request.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(this.certFile, this.certPasswd)); } } else { request = WebRequest.Create(url) as HttpWebRequest; } if (!string.IsNullOrEmpty(ProxyHost)) { System.Net.WebProxy proxy = new WebProxy(ProxyHost); request.Proxy = proxy; } else { //request.Proxy = WebRequest.GetSystemWebProxy(); } request.KeepAlive = true; request.AllowAutoRedirect = false; //request.Timeout = 15000; request.Accept = Accept; request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; //request.Referer = url; foreach (var kv in heads) { //request.Headers.Add(kv.Key, kv.Value.ToString()); request.Headers[kv.Key] = kv.Value.ToString(); } if (request.CookieContainer == null) { request.CookieContainer = new CookieContainer(); } if (RequestWidthCookie) { //附加上Cookie CookieCollection tmpCookies = GetCurrentCookie(); foreach (Cookie cookie in tmpCookies) { cookie.Domain = URI.Host; if (cookie.Value.Length != 0) { request.CookieContainer.Add(cookie); } } } //RequestWidthCookie = true; if ((method != "GET") && (postData.Length > 0)) { request.ContentType = ContentType; request.Method = method; request.ServicePoint.Expect100Continue = false; //EventLog.Log(request.ToJson(), "post"); byte[] b = ContentEncoding.GetBytes(postData); request.ContentLength = b.Length; using (Stream sw = request.GetRequestStream()) { try { sw.Write(b, 0, b.Length); } catch { return(null); } } } return(request); }