标签: httpresponse

HttpWebRequest/HttpResponse:如何在响应中发送数据?

我有一个客户端和一个服务器.

在客户端我有:

HttpWebRequest request = 
    (HttpWebRequest)WebRequest.Create("http://localhost/fa/Default.aspx");
request.Method = "POST";                

byte[] data = Encoding.ASCII.GetBytes(GetSAMLRequestB64());

request.ContentType = "text/xml";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Run Code Online (Sandbox Code Playgroud)

在服务器端,我有:

public void ProcessRequest(HttpContext httpContext) 
{
    HttpResponse response = httpContext.Response;             
    response.Clear();
    response.BufferOutput = true;
    response.StatusCode = 200; // HttpStatusCode.OK;
    response.Write("Hello");
    response.ContentType = "text/xml";
    response.End();
}
Run Code Online (Sandbox Code Playgroud)

客户端收到正确的响应StatusCode.虽然,如果我(int)response.ContentLength;在客户端上,我得到0.我收到响应(客户端)后,我无法读取字符串"Hello".

.net c# httpresponse httpwebrequest

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

ASP.Net MVC2(RTM)中断响应过滤 - "不允许过滤"

我刚刚完成了将项目升级到ASP.Net MVC 2(RTM)的测试运行,期待本月晚些时候发布完整的官方.Net 4.0版本.

我们的应用程序使用最小化器为我们的CSS和JavaScript.为此,它正在使用该HttpResponse.Filter属性来设置自定义过滤器.

随着升级,这个属性的setter抛出HttpException一句话"不允许过滤".看HttpResponse.Filter反射器中的属性显示:

  set
    {
        if (!this.UsingHttpWriter)
        {
            throw new HttpException(SR.GetString("Filtering_not_allowed"));
        }
Run Code Online (Sandbox Code Playgroud)

...

private bool UsingHttpWriter
{
    get
    {
        return ((this._httpWriter != null) && (this._writer == this._httpWriter));
    }
}
Run Code Online (Sandbox Code Playgroud)

很明显,HttpResponse在MVC2中写入输出流的方式发生了变化.有谁知道变化是什么,或者至少是一个解决方法?

编辑: 这似乎很激进.一些进一步的调查显示,ASP.Net MVC 2 RTM使用a System.Web.Mvc.ViewPage.SwitchWriter作为输出属性HttpResponse,而MVC 1使用普通老式HttpWriter.这就解释了为什么抛出异常.

但这并不能解释为什么他们选择完全破坏这一功能. 这个帖子似乎表明这只是暂时的...但这让我非常紧张......毕竟这是RTM.

任何进一步的评论都赞赏此事.

c# asp.net asp.net-mvc httpresponse asp.net-mvc-2

6
推荐指数
1
解决办法
2278
查看次数

PHP get_headers()替代方案

我需要一个PHP脚本来读取每个URL请求的HTTP响应代码.

就像是

$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
Run Code Online (Sandbox Code Playgroud)

问题是get_headers()函数在服务器级别被禁用,作为策略.因此它不起作用.

问题是如何获取URL的HTTP响应代码?

php httpresponse http-headers

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

为什么我们使用Response.ClearHeaders()?

我复制了一个代码片段来将文件发送到浏览器.我不知道为什么我们使用下面写的行因为删除这些在我的开发环境中没有任何区别.

Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Run Code Online (Sandbox Code Playgroud)

任何人都可以简单地分解这些目的和适当性.

谢谢

c# asp.net download httpresponse

6
推荐指数
1
解决办法
7174
查看次数

是否有任何httpresponse标题会将用户重定向到我网站的某个页面?

我是服务员.用户向我发送http请求.我想知道是否有一些http响应标题会使用户网页浏览器自动更新到某些/my/server/page.html

http httpresponse httprequest

6
推荐指数
1
解决办法
5648
查看次数

sendRedirect在时间间隔后避免javascript或php

我该怎么做?

response.sendRedirect("index.jsp");
Run Code Online (Sandbox Code Playgroud)

在没有使用javascript或php的情况下从另一个jsp获得一定时间间隔后?

想法是在"error_page.jsp"中显示错误,然后在一段时间后自动将用户重定向到索引页面.提前致谢.

redirect jsp httpresponse

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

在PHP curl_exec中读取错误主体

可能重复:
使用PHP curl如何获得400响应的响应主体

当使用PHP的curl_exec调用RESTful API时,文档说http://php.net/manual/en/function.curl-exec.php

成功时返回TRUE,失败时返回FALSE.但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将在成功时返回结果,在失败时返回FALSE.

当curl_exec失败时,它返回false.但是,远程服务器可能已尝试返回有用的消息以使用该错误.当它只返回false时,如何使用curl_exec访问此消息?

例如,远程API可能会返回此消息和HTTP 400状态代码:

{"Items":[{"Field":"FirstName","Description":"FirstName is required and cannot be null."}]}
Run Code Online (Sandbox Code Playgroud)

目前我无法读取已返回的此错误消息,而是我得到的所有内容都是false.我也看过curl_info.我确实有返回传输.

在C#中我会使用该exception.Response.GetResponseStream方法并编写:

    private static string Post(WebClient client, string uri, string postData)
    {
        string response = string.Empty;
        try
        {
            response = client.UploadString(uri, "POST", postData);
        }
        catch (WebException ex)
        {
            var stream = ex.Response.GetResponseStream(); // here
            if (stream != null)
            {
                using (var reader = new StreamReader(stream))
                {
                    response = reader.ReadToEnd();
                }
            }
        }
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

如何在PHP中完成?curl_exec是正确的方法还是应该使用别的东西?

php curl httpresponse http-error

6
推荐指数
1
解决办法
2702
查看次数

HTTPResponse为Java中的JSON

我试图从HTTP响应中获取JSONObject.

try
    {   
        GetMethod postMethod = new GetMethod();
        postMethod.setURI(new URI(url, true));
        postMethod.setRequestHeader("Accept", "application/json");
        httpClient.executeMethod(postMethod);
        String resp=postMethod.getResponseBodyAsString();
        org.json.JSONTokener tokener = new org.json.JSONTokener(resp);
        finalResult = new org.json.JSONArray(tokener);

        return finalResult;
    }
Run Code Online (Sandbox Code Playgroud)

但我收到了运行时警告

去缓冲大或未知大小的响应体.使用getResponseBodyAsStream建议来代替.

我应该按照JVM的建议获得响应吗?如果是这样,我怎么能解析它的JSON?

java json httpresponse java-ee

6
推荐指数
1
解决办法
4748
查看次数

使用HTTP/1.1删除NGINX 204响应中的"Connection:keep-alive"响应头

在NGINX提供的任何响应中,如何删除"Connection:keep-alive"标题?

我们为来自NGINX的10个客户提供100B + /月的子100字节响应,用于基于RTB的广告服务.我们正在尝试最小化开销(数据大小)以节省带宽成本.我们不希望实际让NGINX关闭连接,只将其从标头中删除.客户端不是浏览器,将根据HTTP/1.1规范保持连接打开.

我们已经使用HttpHeadersMoreModule从"无操作"响应(204)中成功删除了默认响应头.它看起来像这样:

HTTP/1.1 204 No Content
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

我们想要删除其他标头,因此它看起来像这样:

HTTP/1.1 204 No Content
Run Code Online (Sandbox Code Playgroud)

我们的keepalive_timeout设置为没有第二个值.根据NGINX HttpCoreModule文档,如果没有此[second]参数,nginx不会发送Keep-Alive标头.外出设置是:

keepalive_timeout 60s;
Run Code Online (Sandbox Code Playgroud)

我们尝试过使用和设置http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_headers 和http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_input_headers:

more_clear_headers 'Connection';
more_clear_input_headers 'Connection';
Run Code Online (Sandbox Code Playgroud)

我们也尝试过:

rewrite_by_lua '
    ngx.req.clear_header("Connection")
    ';
Run Code Online (Sandbox Code Playgroud)

甚至是:

more_clear_headers 'Connection*';
Run Code Online (Sandbox Code Playgroud)

但我们仍然看到标题.我们发送的回复如此之多,以至于Connection标题实际上花费了我们每月200美元的费用.

任何帮助表示赞赏.

相关和有用:Nginx和原始标头

http nginx httpresponse keep-alive http-headers

6
推荐指数
1
解决办法
4672
查看次数

如何使用Django HttpResponseRedirect提供规范URL?

这个问题与我刚才问过的问题非常相似:我可以使用Google搜索结果来显示最终的重定向网址吗?,但现在问题是Django特有的.

我的网站的网页网址使用以下格式:

www.mysite.com/id/pretty_title
Run Code Online (Sandbox Code Playgroud)

首页链接到这些页面,但href实际上包含一些参数:

 www.mysite.com/id/?some_ugly_parameters_to_let_me_know_what_search_it_is_from
Run Code Online (Sandbox Code Playgroud)

然后重定向到

www.mysite.com/id/pretty_title
Run Code Online (Sandbox Code Playgroud)

显示页面.

我的问题是 Google的搜索结果显示该页面的链接是丑陋的网址,而不是漂亮的重定向网址.

我所学到的是我需要提供规范链接.但是,当丑陋的网页从未真正存在时,我怎么能这样做,至少不是我写的那个?

服务器端发生的事情是丑陋网址的视图会重定向:

return HttpResponseRedirect(pretty_url)
Run Code Online (Sandbox Code Playgroud)

django httpresponse

6
推荐指数
2
解决办法
3685
查看次数