标签: httpresponse

如何从HttpResponseMessage获取特定的标头值

我正在进行HTTP呼叫.我的响应X-BB-SESSIONHttpResponseMessage对象的标题部分中包含会话代码.如何获取特定标头值?

我正在使用foreach语句迭代所有标头(MSDN链接).但是编译器一直说不能这样做:

foreach statement cannot operate on variables of type
  System.net.http.headers.cachecontrolheadervalue because
  'System.net.http.headers.cachecontrolheadervalue' doesn't contain
  a public definition for 'GetEnumerator'
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的代码:

//Connection code to BaasBox

HttpResponseMessage response = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
    //get the headers
    HttpResponseHeaders responseHeadersCollection = response.Headers;
    foreach (var value in responseHeadersCollection.CacheControl)  --> HERE
    {
        string sTemp = String.Format("CacheControl {0}={1}", value.Name, value.Value);
    } else
{
    Console.WriteLine("X-BB-SESSION: NOT Found");
}
Run Code Online (Sandbox Code Playgroud)

从我试图获取值(X-BB-SESSION值)的标题内容是这样的:

Access-Control-Allow-Origin: *    
Access-Control-Allow-Headers: X-Requested-With    
X-BB-SESSION: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

c# httpresponse httpclient windows-phone-8 baasbox

34
推荐指数
4
解决办法
5万
查看次数

Django将HttpResponseRedirect返回到带参数的url

我的项目中有一种情况,我需要将用户重定向到包含参数的url(它在urls.py中声明如下:

url(r'^notamember/(?P<classname>\w+)/$', 
                           notamember,
                           name='notamember'),)
Run Code Online (Sandbox Code Playgroud)

如何将该参数放入返回HttpResponseRedirect?我尝试过:返回HttpResponseRedirect('/ classroom/notamember/classname'),无论如何,这是愚蠢的,我知道,我不能把'classmane'当作参数.为清楚起见,我的观点是:

def leave_classroom(request,classname):
theclass = Classroom.objects.get(classname = classname)
u = Membership.objects.filter(classroom=theclass).get(member = request.user).delete()
return HttpResponseRedirect('/classroom/notamember/theclass/')
Run Code Online (Sandbox Code Playgroud)

我怎样才能在该网址中包含变量'theclass'?非常感谢!

django url httpresponse urlvariables

31
推荐指数
3
解决办法
5万
查看次数

Web API:HttpResponseMessage中的内容

在我的一个Get请求中,我想返回带有一些内容的HttpResponseMessage.目前我的工作如下:

var header = new MediaTypeHeaderValue("text/xml");
Request.CreateResponse(HttpStatusCode.OK, myObject, header);
Run Code Online (Sandbox Code Playgroud)

但是,由于我使用的是静态请求,因此测试起来非常困难.根据我的阅读,我应该能够做到以下几点:

return new HttpResponseMessage<T>(objectInstance);
Run Code Online (Sandbox Code Playgroud)

但是,似乎无法做到这一点.是因为我使用的是旧版本的WebApi/.NET吗?


另外,我发现您可能会创建如下响应:

var response = new HttpResponseMessage();
response.Content = new ObjectContent(typeof(T), objectInstance, mediaTypeFormatter);
Run Code Online (Sandbox Code Playgroud)

令我困惑的是为什么我必须在这里添加一个mediaTypeFormatter.我在global.asax级别添加了媒体类型格式化程序.

谢谢!

.net httpresponse asp.net-web-api mediatypeformatter

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

使http客户端同步:等待响应

我有一些文件要上传,一些文件失败,因为帖子是异步的而不是同步的..

我正在尝试将此调用作为同步调用..

我想等待回应.

如何将此调用设为同步?

static async Task<JObect> Upload(string key, string url, string 
                                 sourceFile, string targetFormat)
{ 
    using (HttpClientHandler handler = new HttpClientHandler { 
                                           Credentials = new NetworkCredential(key, "") 
                                       })
    using (HttpClient client = new HttpClient(handler))
    {
         var request = new MultipartFormDataContent();
         request.Add(new StringContent(targetFormat), "target_format");
         request.Add(new StreamContent(File.OpenRead(sourceFile)),
                                       "source_file",
                                        new FileInfo(sourceFile).Name);

        using (HttpResponseMessage response = await client.PostAsync(url,
                                                           request).ConfigureAwait(false))

        using (HttpContent content = response.Content)
        {
            string data = await content.ReadAsStringAsync().ConfigureAwait(false);
            return JsonObject.Parse(data);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏!

c# httpresponse dotnet-httpclient

30
推荐指数
2
解决办法
7万
查看次数

返回HttpResponseMessage的Web API最佳方法

我有一个Web API项目,我的方法总是返回HttpResponseMessage.

所以,如果它工作或失败我返回:

没有错误:

return Request.CreateResponse(HttpStatusCode.OK,"File was processed.");
Run Code Online (Sandbox Code Playgroud)

任何错误或失败

return Request.CreateResponse(HttpStatusCode.NoContent, "The file has no content or rows to process.");
Run Code Online (Sandbox Code Playgroud)

当我返回一个对象然后我使用:

return Request.CreateResponse(HttpStatusCode.OK, user);
Run Code Online (Sandbox Code Playgroud)

我想知道如何向HTML5客户端返回更好的封装respose,以便我可以返回有关事务的更多信息等.

我正在考虑创建一个可以封装HttpResponseMessage但也有更多数据的自定义类.

有没有人实现类似的东西?

c# asp.net-mvc json httpresponse asp.net-web-api

29
推荐指数
2
解决办法
8万
查看次数

在ASP.NET MVC2中将http 204"无内容"返回给客户端

在我所拥有的ASP.net MVC 2应用程序中,我想要对post操作返回204 No Content响应.当前我的控制器方法有一个void返回类型,但这会将客户端的响应发送回200 OK,并将Content-Length标头设置为0.如何将响应发送到204?

[HttpPost]
public void DoSomething(string param)
{
    // do some operation with param

    // now I wish to return a 204 no content response to the user 
    // instead of the 200 OK response
}
Run Code Online (Sandbox Code Playgroud)

httpresponse asp.net-mvc-2

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

删除服务器响应标头IIS 8.0/8.5

我们如何在IIS 8.0/8.5中删除服务器头响应?
我当前的服务器报告: Microsoft-IIS/8.0 Microsoft-IIS/8.5
对于IIS 7.0,我使用了URLScan 3.1但是只支持IIS 7.0而不支持8.x.

iis httpresponse http-headers

26
推荐指数
4
解决办法
5万
查看次数

关于在ASP.NET响应流上编写的一些问题

我正在使用ASP.NET HttpHandler进行测试,以便直接在响应流上下载文件,而且我不太确定我的方式.这是一个示例方法,将来文件可以存储在数据库中的BLOB中:

        public void GetFile(HttpResponse response)
    {
        String fileName = "example.iso";
        response.ClearHeaders();
        response.ClearContent();
        response.ContentType = "application/octet-stream";
        response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        using (FileStream fs = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), fileName), FileMode.Open))
        {
            Byte[] buffer = new Byte[4096];
            Int32 readed = 0;

            while ((readed = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                response.OutputStream.Write(buffer, 0, readed);
                response.Flush();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,我不确定这是否正确或有更好的方法来做到这一点.我的问题是:

  1. 当我用浏览器打开网址时,出现"保存文件"对话框......但似乎服务器已经启动将数据推入流中,然后点击"保存",这是正常的吗?
  2. 如果我删除行"response.Flush()",当我用浏览器打开网址时,...我看到网络服务器如何推送数据但是"保存文件"对话框没有出现,(或者至少不是在合理的时间内)为什么?
  3. 当我用WebRequest对象打开url时,我看到HttpResponse.ContentLength是"-1",虽然我可以读取流并获取文件.-1是什么意思?什么时候HttpResponse.ContentLength会显示响应的长度?例如,我有一个方法,用deflate作为二进制流检索一个大的xml,但在那种情况下...当我用WebRequest访问它时,在HttpResponse中我实际上可以看到ContentLength的流长度,为什么?
  4. Byte []数组的最佳长度是什么,我用作缓冲区以获得Web服务器的最佳性能?我读过这是介于4K和8K之间......但是我应该考虑哪些因素做出正确的决定.
  5. 这种方法是否会破坏IIS或客户端内存的使用?还是它实际上正确地缓冲了转移?

很抱歉这么多问题,我在网络开发方面很新:P

干杯.

.net c# asp.net stream httpresponse

25
推荐指数
1
解决办法
8457
查看次数

如何从HttpResponse打印出返回的消息?

我在Android手机上有这个代码.

   URI uri = new URI(url);
   HttpPost post = new HttpPost(uri);
   HttpClient client = new DefaultHttpClient();
   HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)

我有一个asp.net webform应用程序,在页面加载它

 Response.Output.Write("It worked");
Run Code Online (Sandbox Code Playgroud)

我想从HttpReponse中获取此响应并将其打印出来.我该怎么做呢?

我试过response.getEntity().toString()但它似乎打印出内存中的地址.

谢谢

.net java android httpresponse

25
推荐指数
5
解决办法
8万
查看次数

如何在AngularJS 1.2中获取HTTP响应状态代码

使用ngResource在AngularJS 1.2rc(X),我怎么现在得到了状态代码?

RestAPI.save({resource}, {data}, function( response, responseHeaders ) {
});
Run Code Online (Sandbox Code Playgroud)

哪里RestAPI是我的ngResource.

响应具有$promise从服务器返回的对象和资源,但不再具有状态.responseHeaders()如果服务器将状态代码注入头对象,但该函数仅返回真实的返回状态代码,则该函数仅具有状态.所以有些服务器可能会提供服务,而有些服

httpresponse response-headers angularjs

25
推荐指数
3
解决办法
7万
查看次数