本文整理汇总了C#中System.Net.HttpListenerResponse.AddHeader方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerResponse.AddHeader方法的具体用法?C# HttpListenerResponse.AddHeader怎么用?C# HttpListenerResponse.AddHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerResponse
的用法示例。
在下文中一共展示了HttpListenerResponse.AddHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ServeDirectoryListingAsync
private static async Task ServeDirectoryListingAsync(DirectoryInfo root, DirectoryInfo directory, HttpListenerResponse response)
{
StringBuilder listBuilder = new StringBuilder();
foreach (FileInfo file in directory.EnumerateFiles())
{
String target = directory.IsSameDirectory(root) ? file.Name
: Path.Combine(directory.Name, file.Name);
listBuilder.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", target, file.Name);
}
foreach (DirectoryInfo subDirectory in directory.EnumerateDirectories())
{
String target = directory.IsSameDirectory(root) ? subDirectory.Name
: Path.Combine(directory.Name, subDirectory.Name);
listBuilder.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", target, subDirectory.Name);
}
String htmlResponse = String.Format("<ul>{0}</ul>", listBuilder.ToString());
response.ContentType = "text/html";
response.ContentLength64 = htmlResponse.Length;
response.AddHeader("Date", DateTime.Now.ToString("r"));
response.StatusCode = (Int32)HttpStatusCode.OK; // Must be set before writing to OutputStream.
using (StreamWriter writer = new StreamWriter(response.OutputStream))
{
await writer.WriteAsync(htmlResponse).ConfigureAwait(false);
}
}
示例2: Map
public void Map(Response response, HttpListenerResponse result)
{
result.StatusCode = response.StatusCode;
response.Headers.ToList().ForEach(pair => result.AddHeader(pair.Key, pair.Value));
if (response.Body != null)
{
var content = Encoding.UTF8.GetBytes(response.Body);
result.OutputStream.Write(content, 0, content.Length);
}
}
示例3: PrepareOutputStream
/*
* Prepare Output Stream, using gzip if supported */
protected Stream PrepareOutputStream(HttpListenerRequest request, HttpListenerResponse response)
{
string accept = request.Headers.Get("Accept-Encoding");
Stream outputStream = response.OutputStream;
if (accept != null && accept.Contains("gzip"))
{
response.AddHeader("Content-Encoding", "gzip");
outputStream = new GZipStream(response.OutputStream, CompressionMode.Compress);
}
return outputStream;
}
示例4: ResponseText
public static async Task ResponseText(HttpListenerResponse response, string localFilePath, string contentType)
{
var text = File.ReadAllText(localFilePath);
response.ContentType = contentType;
response.ContentEncoding = Encoding.UTF8;
response.AddHeader("Charset", Encoding.UTF8.WebName);
var textBytes = Encoding.UTF8.GetBytes(text);
response.ContentLength64 = textBytes.LongLength;
await response.OutputStream.WriteAsync(textBytes, 0, textBytes.Length);
}
示例5: deserializeResponse
public static void deserializeResponse(string response, HttpListenerResponse originalResponse)
{
var headerResponse = JsonConvert.DeserializeObject<HeaderRespons> (response);
if (relayIdSet.Contains (headerResponse.rid))
relayIdSet.Remove (headerResponse.rid);
originalResponse.StatusCode = headerResponse.statusCode;
foreach (var h in headerResponse.headers) {
if (HeaderUtil.isEndToEndHeader (h.Key))
originalResponse.AddHeader (h.Key, h.Value);
}
//originalResponse.OutputStream.Write (headerResponse.body, 0, headerResponse.body.Length);
}
示例6: PrepareContext
void PrepareContext(HttpListenerResponse tragetRsp, HttpWebResponse sourceRsp)
{
tragetRsp.ContentEncoding = Encoding.GetEncoding(sourceRsp.CharacterSet);
tragetRsp.ContentType = sourceRsp.ContentType;
foreach (string k in sourceRsp.Headers)
foreach (string v in sourceRsp.Headers.GetValues(k))
{
if (string.Equals(k, "Content-Length", StringComparison.OrdinalIgnoreCase))
continue;
tragetRsp.AddHeader(k, v);
}
}
示例7: SendResponse
public static void SendResponse(HttpListenerResponse response, string pluginName, string content)
{
CoreManager.ServerCore.GetStandardOut().PrintDebug("WebAdmin Response [" + pluginName + "]: " + content);
byte[] buffer = Encoding.UTF8.GetBytes(content);
response.StatusCode = (int) HttpStatusCode.OK;
response.StatusDescription = "OK";
response.ContentType = "text/html; charset=UTF-8";
response.ContentLength64 = buffer.Length;
response.ContentEncoding = Encoding.UTF8;
response.AddHeader("plugin-name", pluginName);
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
response.Close();
}
示例8: SendResponse
public void SendResponse(HttpListenerRequest request, HttpListenerResponse response)
{
var stream = new FileLoader(request.Url.AbsolutePath).LoadStream();
response.ContentLength64 = stream.Length;
response.SendChunked = false;
response.ContentType = request.ContentType;
response.AddHeader("Content-disposition", "attachment; filename=" + request.RawUrl.Remove(0, 1));
writeTo(stream, response.OutputStream);
response.StatusCode = (int)HttpStatusCode.OK;
response.StatusDescription = "OK";
stream.Close();
response.Close();
Console.WriteLine("200");
}
示例9: ConvertNancyResponseToResponse
private static void ConvertNancyResponseToResponse(Response nancyResponse, HttpListenerResponse response)
{
foreach (var header in nancyResponse.Headers)
{
response.AddHeader(header.Key, header.Value);
}
foreach (var nancyCookie in nancyResponse.Cookies)
{
response.Cookies.Add(ConvertCookie(nancyCookie));
}
response.ContentType = nancyResponse.ContentType;
response.StatusCode = (int)nancyResponse.StatusCode;
using (var output = response.OutputStream)
{
nancyResponse.Contents.Invoke(output);
}
}
示例10: SaveInfo
public Ansver SaveInfo(HttpListenerRequest request, HttpListenerResponse response)
{
Stream inputStream = request.InputStream;
Console.WriteLine("������ ����������� �����������");
Console.WriteLine("���������� ������ �� ������:");
string result = "";
int last;
string uriPath = request.Url.AbsolutePath.Split('/')[2];
try
{
byte[] ansver = new byte[request.ContentLength64];
request.InputStream.Read(ansver, 0, (int)request.ContentLength64);
result = Encoding.UTF8.GetString(ansver);
Console.WriteLine(result);
Directory.CreateDirectory(path + uriPath);
List<string> allfiles = new List<string>(Directory.GetFiles(path + uriPath, "*.txt"));
allfiles.Sort();
last = this.GetLast(allfiles);
StreamWriter writer = new StreamWriter(path + uriPath + "/" + (last).ToString() + ".txt",false, Encoding.UTF8);
writer.Write(result);
Console.WriteLine("������ ���������.");
writer.Close();
response.AddHeader("id", last.ToString());
response.StatusCode = (int) HttpStatusCode.Created;
return new Ansver(null, response);
}
catch (Exception exception)
{
Console.WriteLine("������ ����������, ��������� ������: " + exception.Message);
response.StatusCode = (int) HttpStatusCode.InternalServerError;
return new Ansver(null, response);
}
}
示例11: ConvertNancyResponseToResponse
private void ConvertNancyResponseToResponse(Response nancyResponse, HttpListenerResponse response)
{
foreach (var header in nancyResponse.Headers)
{
response.AddHeader(header.Key, header.Value);
}
foreach (var nancyCookie in nancyResponse.Cookies)
{
response.Headers.Add(HttpResponseHeader.SetCookie, nancyCookie.ToString());
}
if (nancyResponse.ReasonPhrase != null)
{
response.StatusDescription = nancyResponse.ReasonPhrase;
}
if (nancyResponse.ContentType != null)
{
response.ContentType = nancyResponse.ContentType;
}
response.StatusCode = (int)nancyResponse.StatusCode;
if (configuration.AllowChunkedEncoding)
{
OutputWithDefaultTransferEncoding(nancyResponse, response);
}
else
{
OutputWithContentLength(nancyResponse, response);
}
}
示例12: Exception
void Exception (HttpListenerResponse response, Exception exception)
{
var id = CreateExceptionId ();
exceptions [id] = exception;
response.AddHeader (ExceptionHeaderName, id.ToString ());
if (exception == null)
return;
response.StatusCode = 500;
using (var writer = new StreamWriter (response.OutputStream)) {
writer.WriteLine (string.Format ("EXCEPTION: {0}", exception));
}
response.Close ();
}
示例13: ServeFile
static bool ServeFile(string path, HttpListenerResponse httpResponse)
{
if(!File.Exists(path))
{
return false;
}
// http://stackoverflow.com/a/13386573/4264
using(var fs = File.OpenRead(path))
{
httpResponse.StatusCode = (int)HttpStatusCode.OK;
httpResponse.ContentLength64 = fs.Length;
httpResponse.SendChunked = false;
httpResponse.ContentType = GetContentType(Path.GetExtension(path));
if(httpResponse.ContentType == "application/octet-stream")
{
httpResponse.AddHeader("Content-disposition", "attachment; filename=" +
Path.GetFileName(path));
}
byte[] buffer = new byte[64 * 1024];
int read;
using(BinaryWriter bw = new BinaryWriter(httpResponse.OutputStream))
{
while((read = fs.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, read);
bw.Flush(); //seems to have no effect
}
}
}
return true;
}
示例14: SendZippedResponse
public void SendZippedResponse(HttpListenerResponse response, ByteResponseData data)
{
response.AddHeader("Content-Encoding", "gzip");
response.ContentType = data.ContentType.GetValue();
byte[] buffer = data.Content;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress))
{
zip.Write(buffer, 0, buffer.Length);
}
buffer = ms.ToArray();
}
WriteAndFlushResponse(response, buffer);
}
示例15: ProcessRequest
private void ProcessRequest(HttpListenerRequest request, HttpListenerResponse response)
{
response.AddHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
response.AddHeader("Pragma", "no-cache");
IRequestHandler handler = AssignRequestHandler(request);
if (handler != null)
{
handler.Process(request, response);
}
else
{
FinalizeIgnoredResponse(response, 404, "Resource Not Found");
}
}