本文整理汇总了C#中IHttpClientContext.Respond方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpClientContext.Respond方法的具体用法?C# IHttpClientContext.Respond怎么用?C# IHttpClientContext.Respond使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpClientContext
的用法示例。
在下文中一共展示了IHttpClientContext.Respond方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessRequest
private void ProcessRequest(IHttpClientContext context, IHttpRequest request)
{
IHttpResponse response = request.CreateResponse(context);
try
{
foreach (IRule rule in _rules)
{
if (!rule.Process(request, response))
continue;
response.Send();
return;
}
// load cookies if the exist.
RequestCookies cookies = request.Headers["cookie"] != null
? new RequestCookies(request.Headers["cookie"])
: new RequestCookies(string.Empty);
request.SetCookies(cookies);
IHttpSession session;
if (cookies[_sessionCookieName] != null)
{
string sessionCookie = cookies[_sessionCookieName].Value;
// there's a bug somewhere which messes up headers which can render the session cookie useless.
// therefore let's consider the session cookie as not set if that have happened.
if (sessionCookie.Length > 40)
{
_logWriter.Write(this, LogPrio.Error, "Session cookie is invalid: " + sessionCookie);
cookies.Remove(_sessionCookieName);
_sessionStore.Remove(sessionCookie); // free the session cookie (and thus generating a new one).
session = _sessionStore.Create();
}
else
session = _sessionStore.Load(sessionCookie) ??
_sessionStore.Create(sessionCookie);
}
else
session = _sessionStore.Create();
HandleRequest(context, request, response, session);
}
catch (Exception err)
{
if (_exceptionHandler == null)
#if DEBUG
throw;
#else
{
WriteLog(LogPrio.Fatal, err.Message);
return;
}
#endif
_exceptionHandler(this, err);
Exception e = err;
while (e != null)
{
if (e is SocketException)
return;
e = e.InnerException;
}
try
{
#if DEBUG
context.Respond("HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", err.ToString(), "text/plain");
#else
context.Respond("HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error");
#endif
}
catch (Exception err2)
{
LogWriter.Write(this, LogPrio.Fatal, "Failed to respond on message with Internal Server Error: " + err2);
}
}
}
示例2: SendResponse
void SendResponse(IHttpClientContext context, IHttpRequest request, IHttpResponse response, List<EventQueueEvent> eventsToSend)
{
response.Connection = request.Connection;
if (eventsToSend != null)
{
OSDArray responseArray = new OSDArray(eventsToSend.Count);
// Put all of the events in an array
for (int i = 0; i < eventsToSend.Count; i++)
{
EventQueueEvent currentEvent = eventsToSend[i];
OSDMap eventMap = new OSDMap(2);
eventMap.Add("message", OSD.FromString(currentEvent.Name));
eventMap.Add("body", currentEvent.Body);
responseArray.Add(eventMap);
}
// Create a map containing the events array and the id of this response
OSDMap responseMap = new OSDMap(2);
responseMap.Add("events", responseArray);
responseMap.Add("id", OSD.FromInteger(currentID++));
// Serialize the events and send the response
string responseBody = OSDParser.SerializeLLSDXmlString(responseMap);
Logger.Log.Debug("[EventQueue] Sending " + responseArray.Count + " events over the event queue");
context.Respond(HttpHelper.HTTP11, HttpStatusCode.OK, "OK", responseBody, "application/xml");
}
else
{
//Logger.Log.Debug("[EventQueue] Sending a timeout response over the event queue");
// The 502 response started as a bug in the LL event queue server implementation,
// but is now hardcoded into the protocol as the code to use for a timeout
context.Respond(HttpHelper.HTTP10, HttpStatusCode.BadGateway, "Upstream error:", "Upstream error:", null);
}
}
示例3: HandleHTTPRequest_NoLock
//.........这里部分代码省略.........
if (pathAndQuery.StartsWith(config.DescriptionPathBase))
{
string acceptLanguage = request.Headers.Get("ACCEPT-LANGUAGE");
CultureInfo culture = GetFirstCultureOrDefault(acceptLanguage, CultureInfo.InvariantCulture);
string description = null;
DvDevice rootDevice;
lock (_serverData.SyncObj)
if (config.RootDeviceDescriptionPathsToRootDevices.TryGetValue(pathAndQuery, out rootDevice))
description = rootDevice.BuildRootDeviceDescription(_serverData, config, culture);
else if (config.SCPDPathsToServices.TryGetValue(pathAndQuery, out service))
description = service.BuildSCPDDocument(config, _serverData);
if (description != null)
{
IHttpResponse response = request.CreateResponse(context);
response.Status = HttpStatusCode.OK;
response.ContentType = "text/xml; charset=utf-8";
if (!string.IsNullOrEmpty(acceptLanguage))
response.AddHeader("CONTENT-LANGUAGE", culture.ToString());
using (MemoryStream responseStream = new MemoryStream(UPnPConsts.UTF8_NO_BOM.GetBytes(description)))
CompressionHelper.WriteCompressedStream(acceptEncoding, response, responseStream);
SafeSendResponse(response);
return;
}
}
}
else if (request.Method == "POST")
{ // POST of control messages
if (config.ControlPathsToServices.TryGetValue(pathAndQuery, out service))
{
string contentType = request.Headers.Get("CONTENT-TYPE");
string userAgentStr = request.Headers.Get("USER-AGENT");
IHttpResponse response = request.CreateResponse(context);
int minorVersion;
if (string.IsNullOrEmpty(userAgentStr))
minorVersion = 0;
else if (!ParserHelper.ParseUserAgentUPnP1MinorVersion(userAgentStr, out minorVersion))
{
response.Status = HttpStatusCode.BadRequest;
SafeSendResponse(response);
return;
}
string mediaType;
Encoding encoding;
if (!EncodingUtils.TryParseContentTypeEncoding(contentType, Encoding.UTF8, out mediaType, out encoding))
throw new ArgumentException("Unable to parse content type");
if (mediaType != "text/xml")
{ // As specified in (DevArch), 3.2.1
response.Status = HttpStatusCode.UnsupportedMediaType;
SafeSendResponse(response);
return;
}
response.AddHeader("DATE", DateTime.Now.ToUniversalTime().ToString("R"));
response.AddHeader("SERVER", UPnPConfiguration.UPnPMachineInfoHeader);
response.AddHeader("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
string result;
HttpStatusCode status;
try
{
CallContext callContext = new CallContext(request, context, config);
status = SOAPHandler.HandleRequest(service, request.Body, encoding, minorVersion >= 1, callContext, out result);
}
catch (Exception e)
{
UPnPConfiguration.LOGGER.Warn("Action invocation failed", e);
result = SOAPHandler.CreateFaultDocument(501, "Action failed");
status = HttpStatusCode.InternalServerError;
}
response.Status = status;
using (MemoryStream responseStream = new MemoryStream(encoding.GetBytes(result)))
CompressionHelper.WriteCompressedStream(acceptEncoding, response, responseStream);
SafeSendResponse(response);
return;
}
}
else if (request.Method == "SUBSCRIBE" || request.Method == "UNSUBSCRIBE")
{
GENAServerController gsc;
lock (_serverData.SyncObj)
gsc = _serverData.GENAController;
if (gsc.HandleHTTPRequest(request, context, config))
return;
}
else
{
context.Respond(HttpHelper.HTTP11, HttpStatusCode.MethodNotAllowed, null);
return;
}
}
// Url didn't match
context.Respond(HttpHelper.HTTP11, HttpStatusCode.NotFound, null);
}
catch (Exception e)
{
UPnPConfiguration.LOGGER.Error("UPnPServer: Error handling HTTP request '{0}'", e, uri);
IHttpResponse response = request.CreateResponse(context);
response.Status = HttpStatusCode.InternalServerError;
SafeSendResponse(response);
}
}
示例4: HandleHTTPRequest
protected void HandleHTTPRequest(IHttpClientContext context, IHttpRequest request)
{
Uri uri = request.Uri;
string hostName = uri.Host;
string pathAndQuery = uri.PathAndQuery;
try
{
// Handle different HTTP methods here
if (request.Method == "NOTIFY")
{
foreach (DeviceConnection connection in _connectedDevices.Values)
{
if (!NetworkHelper.HostNamesEqual(hostName,
NetworkHelper.IPAddrToHostName(connection.GENAClientController.EventNotificationEndpoint.Address)))
continue;
if (pathAndQuery == connection.GENAClientController.EventNotificationPath)
{
IHttpResponse response = request.CreateResponse(context);
response.Status = connection.GENAClientController.HandleUnicastEventNotification(request);
response.Send();
return;
}
}
}
else
{
context.Respond(HttpHelper.HTTP11, HttpStatusCode.MethodNotAllowed, null);
return;
}
// Url didn't match
context.Respond(HttpHelper.HTTP11, HttpStatusCode.NotFound, null);
}
catch (Exception) // Don't log the exception here - we don't care about not being able to send the return value to the client
{
IHttpResponse response = request.CreateResponse(context);
response.Status = HttpStatusCode.InternalServerError;
response.Send();
}
}