问题标签 [httpcontent]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
210863 浏览

c# - HttpContent.ReadAsAsync 在哪里?

我在网络上使用新HttpClient对象(作为新 Web API 的一部分)的大量示例中看到应该有HttpContent.ReadAsAsync<T>方法。但是MSDN没有提到这个方法,IntelliSense也没有找到。

它去了哪里,我该如何解决?

0 投票
3 回答
136190 浏览

c# - 在 WebApi 控制器中读取 HttpContent

如何在 MVC webApi 控制器操作中读取 PUT 请求的内容。

我在这里得到空字符串:(

我需要做的是:找出在初始请求中修改/发送了“哪些属性”(这意味着如果Contact对象有 10 个属性,并且我只想更新其中的 2 个,我发送和对象只有两个属性,像这样的东西:

预期的最终结果是

0 投票
2 回答
13988 浏览

c# - HttpContent.ReadAsStringAsync 导致请求挂起(或其他奇怪的行为)

我们正在构建一个高度并发的 Web 应用程序,最近我们开始广泛使用异步编程(使用 TPL 和async/ await)。

我们有一个分布式环境,其中应用程序通过 REST API(建立在 ASP.NET Web API 之上)相互通信。在一个特定的应用程序中,我们有一个DelegatingHandler在调用后base.SendAsync(即,在计算响应之后)将响应记录到文件中。我们在日志中包含响应的基本信息(状态码、标头和内容):

问题是这样的:当代码content.ReadAsStringAsync().Result在繁重的服务器负载下到达时,请求有时会挂在 IIS 上。当它这样做时,它有时会返回响应——但就像没有响应一样挂在 IIS 上——或者在其他时候它永远不会返回。

我也尝试使用阅读内容ReadAsByteArrayAsync然后将其转换为String,但没有成功。

当我将代码转换为始终使用异步时,我得到了更奇怪的结果:

调用后nowHttpContext.Current为 null content.ReadAsStringAsync(),并且对于所有后续请求它一直为 null!我知道这听起来令人难以置信——我花了一些时间和三位同事的在场才接受这真的发生了。

这是某种预期的行为吗?我在这里做错了吗?

0 投票
1 回答
2853 浏览

c# - C# 无法将欧元符号打印到文件中(使用 Excel 打开时)

我对进入 Web api 控制器的 get 方法有疑问。此方法返回一个 HttpResponseMessage 对象,该对象具有一个 HttpContent 和一个 csv 文件,其中包含欧元符号。当该方法返回文件时,不会打印欧元符号。该方法的代码如下:

当我打开文件时,欧元符号显示不正确。你能给我一个答案吗?

非常感谢。

0 投票
3 回答
499295 浏览

c# - 如何为我的 HttpClient PostAsync 第二个参数设置 HttpContent?

PostAsync需要另一个HttpContent参数。

我该如何设置HttpContent?没有适用于 Windows Phone 8 的文档。

如果我这样做GetAsync,效果很好!但它需要与 key="bla", something="yay" 的内容一起发布

//编辑

非常感谢您的回答......这很好用,但这里仍然有一些不确定:

我假设的数据“test=something”会在 api 端作为发布数据“test”获取,显然它不会。另一方面,我可能需要通过发布数据发布整个对象/数组,所以我认为 json 最好这样做。关于如何获取发布数据的任何想法?

也许是这样的:

0 投票
2 回答
8892 浏览

c# - 使用 Microsoft.Net.Http 将文件发送到服务

我有一个方法:

那是将文件发送到 WCF 服务,但是查看帖子的 Wireshark 日志,没有附加 fileStream,只是附加了文件名。我需要做其他事情吗?

0 投票
13 回答
107251 浏览

c# - 重试 HttpClient 不成功的请求

我正在构建一个给定 HttpContent 对象的函数,它将发出请求并在失败时重试。但是,我收到异常说 HttpContent 对象在发出请求后被释放。无论如何要复制或复制 HttpContent 对象,以便我可以发出多个请求。

(显然我不会无限期地尝试,但上面的代码基本上是我想要的)。

它产生这个异常

无论如何要复制 HttpContent 对象或重用它吗?

0 投票
1 回答
1659 浏览

ajax - Allow my Tumblr blog's content to be accessed by another page

I'm attempting to copy all of the actual content from my Tumblr blog using a script I wrote on a different web page, but I'm having a bit of trouble with gaining access to the content. My ajax call is as follows:

but as it is the console gives me an error of "Resource interpreted as Script but transferred with MIME type text/html" which I looked into here and changed the corresponding meta tag in the HTML of my blog to <meta http-equiv="Content-Type" content="application/javascript; charset=utf-8" /> with no success

I also tried using dataType: 'html' (which makes more sense to me) but I was getting a console error of "Origin is not allowed by Access-Control-Allow-Origin" which I also looked into and added a meta tag to my Tumblr blog with <meta Access-Control-Allow-Origin="*" />, but again didn't succeed

Here is a jsFiddle to work with

Does my approach not work because Tumblr as a whole does not allow changes to Access-Control? If so, how might I work around the issue? If not, what am I doing wrong?

MAJOR EDIT (based on mikedidthis's helpful comments)

It seems that I am not able to do this without a Tubmlr API, so I obtained an API key and now have access to the json results that the API sends out. I am able to get a jsonp object using the API key to in the console. My javascript at the moment:

This SO post was helpful in understanding how I can change data on my Tubmlr page from the source and find out basic information about the site, but not about how to obtain actual data from individual posts. I tried looking through the results object and was unable to find any data related to posts, nor was I able to append the results to the jsfiddle. So my questions now are, "Can I copy data (say the written text in a post) from individual posts using this approach? If so, how? If not, what other approach should I use?"

0 投票
2 回答
12946 浏览

c# - 如何将带有自定义标头的任意 JSON 数据发送到 REST 服务器?

TL;DR -- 如何将 JSON 字符串发送到带有 auth 标头的 REST 主机?我尝试了 3 种不同的方法,找到了一种适用于匿名类型的方法。为什么我不能使用匿名类型?我需要设置一个名为“Group-Name”的变量,连字符不是有效的 C# 标识符。

背景

我需要 POST JSON,但无法正确获取正文和内容类型

功能 #1 - 适用于匿名类型

内容类型和数据是正确的,但我不想使用匿名类型。我想用一个字符串

输出#1

使用 AnonymousTypes + PostAsJsonAsync 时内容类型和数据是正确的,但我不想使用匿名类型。

功能 #2 - 没有按预期工作

获取一个字符串并将其放入 StringContent 对象中。这具有更改内容类型的副作用。

输出#2

使用 StringContent + PostAsync 时内容类型错误

功能 #3 - 无法按预期工作

由于我知道PostAsJsonAsync正确设置了 contentType,所以让我们使用该方法。(不起作用)

输出#3

内容类型正确,使用 StringContent + PostAsJsonAsync 时 POST 正文错误

问题

我要做的就是将 JSON 作为字符串或在运行时定义的动态对象发送到服务器,HTTP 内容类型正确,并带有特殊的“Auth-Token”标头。

任何示例,如果不使用 WebAPI,例如 servicestack 或其他任何东西,都很酷。

0 投票
1 回答
1267 浏览

java - 打开 HTTP 连接后如何识别内容类型?

我正在打开一个到我在程序中生成的 URL 的 HTTP 连接:

现在,case 2我想确定内容类型。例如:

我无法使用 URL 结构来确定这一点。例如:

我知道如何使用InputStream对象读取内容,但是:

  1. 如果可能,我希望避免获取内容本身。

  2. 我看不出它如何帮助我确定内容类型。

任何想法将不胜感激......谢谢。