标签: httpresponse

在Django中,如何在HttpResponse中获取转义的html?

我的一个视图中的以下代码返回未转义的html字符串,由于它是一个Ajax请求,因此无法在前端解析.

return render_to_response(template_name, {
        'form': form,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

纠正这个问题最简单的方法是什么?提前致谢..

python django escaping httpresponse django-templates

10
推荐指数
2
解决办法
1万
查看次数

如何在Django中引发410错误

我想为我的一些Django页面返回410错误,而不是返回404.基本上,raise Http404('some error message')我不想打电话,而是打电话给raise Http410('some error message')快捷方式.

我很困惑因为在django.http中,函数Http404很简单:

class Http404(Exception):
    pass
Run Code Online (Sandbox Code Playgroud)

所以,如果我做同样的事情并创建我的Http410函数,我会认为它看起来像:

class Http410(Exception):
    pass
Run Code Online (Sandbox Code Playgroud)

但是,这样做会返回异常,但会提供500错误页面.如何重新创建Http404异常的魔力?我应该注意,我需要从我的模型(而不是视图)中引发异常,所以我不能只返回一个HttpResponseGone.

提前致谢!

更新:我完全了解HttpResponseGone,并在我原来的问题中提到了这一点.我已经知道如何在我的观点中返回这个.我的问题是:你如何提出一个Http 410异常与你如何引发Http 404异常类似?我希望能够在任何地方提出这个例外,而不仅仅是在我的观点中.谢谢!

python django error-handling httpresponse http-status-code-410

10
推荐指数
3
解决办法
9027
查看次数

如何获取Django响应对象的内容长度?

在Django中,我尝试记录请求和响应内容长度,这与Django服务器打印到stderr的内容完全相同.

[05/Apr/2011 22:59:08] "GET /pages/ HTTP/1.1" 200 332161
[05/Apr/2011 22:59:15] "GET /pages/12 HTTP/1.1" 301 0
[05/Apr/2011 22:59:15] "GET /pages/12/ HTTP/1.1" 200 361474
[05/Apr/2011 22:59:16] "GET /pages/12/load/tags/ HTTP/1.1" 200 13899
[05/Apr/2011 22:59:16] "GET /pages/12/load/comments/ HTTP/1.1" 200 82
Run Code Online (Sandbox Code Playgroud)

所以,我写了一个简单的中间件如下,但是,'Content-Length'的值总是空的.

class LogHttpResponse(object):
    def process_response(self, request, response):
        import datetime  
        print response.items()
        time_text = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
        print '[%s] "%s %s" %d %s' % (time_text, request.method, request.path, 
                                      response.status_code, 
                                      response.get('Content-Length', ''))
        return response 
Run Code Online (Sandbox Code Playgroud)

我通过fire-debug检查过,响应头中有'Content-Length'.但是中间件中没有"内容长度","print response.items()"显示:

[('Content-Type', 'text/html; charset=utf-8')]
Run Code Online (Sandbox Code Playgroud)

中间件订单有什么问题吗?

django http httpresponse content-length

10
推荐指数
2
解决办法
9609
查看次数

在HttpResponse execute for android中,主机名可能不为null

我收到错误"目标主机不能为空,或在参数中设置".

  • I DO具有Internet权限在我的清单文件
  • 我把"http://"放在我的网址之前
  • 我会对 URL进行编码

这是我的代码:

   String url = "http://maps.google.com/maps/api/directions/json?origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
   HttpClient httpclient = new DefaultHttpClient();
   String goodURL = convertURL(url);//change weird characters for %etc
   HttpPost httppost = new HttpPost(goodURL);
   HttpResponse response = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)

在第5行(上面的最后一行),我的程序抛出异常.这是确切的错误:

java.lang.IllegalArgumentException: Host name may not be null
Run Code Online (Sandbox Code Playgroud)

我在方法convertURL中编码我的字符串...

goodURL = http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false

有什么建议?谢谢!

android httpresponse http-post illegalargumentexception

10
推荐指数
1
解决办法
3万
查看次数

如何从HttpResponseDecorator获取原始响应和URL

在REST客户端的HTTP生成器返回HttpResponseDecorator.如何从中获取原始响应(用于记录目的)?

编辑(一些代码可能很方便):

    withRest(uri: domainName) {
        def response = post(path: 'wsPath', query: [q:'test'])
        if (!response.success) {
            log.error "API call failed. HTTP status: $response.status"
            // I want to log raw response and URL constructed here
        }
Run Code Online (Sandbox Code Playgroud)

groovy httpresponse httpbuilder

10
推荐指数
1
解决办法
7936
查看次数

如何从Web Api获取自定义错误消息到jQuery.ajax?

此代码使用Microsoft Web Api Http堆栈和jQuery.

如何获取由jQuery的deferred.fail()显示的由HttpError参数创建的自定义错误消息给CreateErrorResponse ()?

在ApiController中为测试目的创建错误响应的示例:

public HttpResponseMessage Post(Region region)
{
    var error = new HttpError("Failure to lunch.");
    return this.Request.CreateErrorResponse(
               HttpStatusCode.InternalServerError, 
               error);
}
Run Code Online (Sandbox Code Playgroud)

这是一个减少客户端,试图找到要显示的错误消息,"未能午餐.".

$.ajax({
    type: 'POST',
    url: 'api/region',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(region)
})
.fail(function (jqXhr, textStatus, errorThrown) {
    alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText);
});
Run Code Online (Sandbox Code Playgroud)

将显示的是:

"错误:内部服务器错误:{full stack here}"

我想要的是:

"没有吃午饭."

c# jquery httpresponse jqxhr asp.net-web-api

10
推荐指数
1
解决办法
8423
查看次数

强制PDF下载时使用Response.End

最近我们遇到了一个问题,其中一个开发人员将代码行从使用HttpResponse.End改为使用HttpApplication.CompleteRequest时强制PDF以类似于以下方式下载:https: //stackoverflow.com/a/8590579/3856039

这样做会导致某些PDF由于不间断的空间问题而无法下载,因此代码已更改回使用状态HttpResponse.End.

然而,在帮助我的同事进行一些研究时,我遇到了以下问题: Response.End()被认为是有害的吗?

链接到:https: //blogs.msdn.microsoft.com/aspnetue/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation/

鉴于MSDN博客文章中记录的内容,听起来好像使用HttpResponse.End是错误的方法,所以我想知道它是否甚至需要或是否有更好的方法?

c# asp.net httpresponse

10
推荐指数
1
解决办法
777
查看次数

从servlet设置错误消息和正文

我想写一个servlet,它将返回一个http响应,如下所示:

HTTP/1.1 500 <short custom message>
Content-Length: ...

<longer custom message>
Run Code Online (Sandbox Code Playgroud)

原因是我希望程序化客户端能够处理响应消息以获取特定响应,但我还想用更长的解释来填充响应主体,以便使用浏览器轻松命中.

现在,HttpServletResponse有一个sendError(int,String)方法,允许我指定错误代码和消息.javadocs只说该消息将嵌入某种html页面,但没有关于设置http响应消息.调用此方法后,不允许您在响应中写入任何其他内容.在我的测试中(使用jetty),消息用于http响应和html主体,这对我来说没问题,除了我想指定两个不同的字符串,我不认为http响应的设置使用不同的实现保证消息.

还有一个setStatus(int)方法,您可以使用任何代码调用,然后您可以编写自己的html主体.这是关闭的,除了您不能指定http响应消息.

最后,有一个setStatus(int,String)方法实际上完全符合我的要求,但由于某种歧义而被弃用.我假设一些servlet容器正在将消息写入响应主体并关闭响应.

除了使用弃用的方法,我假设我已经搞砸了,但我很好奇是否有其他人知道任何技巧?

java servlets httpresponse

9
推荐指数
2
解决办法
3万
查看次数

如何在ASP.NET代码隐藏下载文件时强制Chrome打开"打开文件对话框"?

我有一个页面,它将binari文件,pdf,word或excel发送到Web浏览器.在firefox中,IE都打开一个对话框,询问你对这个文件做了什么,"打开"或"保存"

在此输入图像描述

但Chrome直接将其保存到您的计算机上.

是否有可能让Chrome问您要对此文件做什么,在将文件发送到浏览器之前将一些元数据放入Web响应中?

c# asp.net google-chrome httpresponse

9
推荐指数
1
解决办法
1万
查看次数

最适合"正在进行的作业"的HTTP状态代码

什么是最合适的HTTP状态代码,给客户端意味着"您的请求很好,但它仍在进行中;请在同一位置稍后查看."

例如,假设客户端提交初始请求以启动繁重查询,并且服务器立即返回客户端可以定期轮询结果的URL.如果客户端在作业完成之前调用此URL,那么返回的最合适的HTTP状态代码是什么?

202接受将是我的第一次冲动.这是最好的一个,还是在REST接口中有更好的用于此目的的更好的一个?

rest http response httpresponse http-status-codes

9
推荐指数
1
解决办法
6749
查看次数