问题标签 [system.net.httpwebrequest]

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 投票
0 回答
50 浏览

windows-phone - 在 Windows Phone 中获取默认 UserAgent

有没有办法从 HttpClient 或 HttpRequestMessage.Headers 中获取默认的用户代理字符串?

还是只能通过使用 Webview 类对象来实现?

0 投票
1 回答
303 浏览

httpwebrequest - 使用 C#.Net HttpWebRequest 的疯狂身份验证错误:“您必须指定 environment_key 参数”

我目前正在使用与基本 http 身份验证一起使用的 Spreedly REST API。

起初,我这样设置我的凭据:

它适用于许多 API 资源,包括 POST 新网关、GET 网关、PUT 网关数据,甚至编辑网关。

/v1/payment_methods.<format>当我试图在他们的资源上发布一个新的 payment_method 时,我碰壁了。

我得到的错误:

在验证了很多东西之后,我根据他们的示例使用 curl 进行了尝试,并且成功了。

问题似乎在于我在 HttpWebRequest 中设置凭据的方式,即使它与我用于其他工作资源的代码相同。

我尝试使用 CredentialCache,因为我可以指定“基本”,但它以同样的方式失败:

0 投票
1 回答
138 浏览

c# - 用于可移植跨平台的 c# .NET 4.5 HttpWebRequest

我无法更改 HttpWebRequest 的用户代理:

总是抛出异常。有人知道如何解决这个问题吗?

0 投票
1 回答
191 浏览

c# - 来自 GetResponseStream 的数据

如何获取从 HttpWebRequest 收到的数据的 sub_category_en?我只想要收到的数据的两到三个字段。

02-14 16:33:56.793 I/mono-stdout( 8974): { 02-14 16:33:56.795 I/mono-stdout( 8974): "active": 1, 02-14 16:33:56.797 I/mono-stdout( 8974): "child_level": 1, 02-14 16:33:56.798 I/mono-stdout( 8974): "create_date": "2015-08-27T17:24:58.19+03:00", 02-14 16:33:56.798 I/mono-stdout( 8974): "description_ar": "", 02-14 16:33:56.800 I/mono-stdout( 8974): "description_en": "", 02-14 16:33:56.802 I/mono-stdout( 8974): "has_child": 1, 02-14 16:33:56.805 I/mono-stdout( 8974): "id": 1881, 02-14 16:33:56.805 I/mono-stdout( 8974): "id_category": 3, 02-14 16:33:56.808 I/mono-stdout( 8974): "id_parent": 0, 02-14 16:33:56.811 I/mono-stdout( 8974): "id_parents": "0", 02-14 16:33:56.811 I/mono-stdout( 8974): "id_user": 1, 02-14 16:33:56.811 I/mono-stdout( 8974): "last_update": "2015-08-27T17:24:58.19+03:00", 02-14 16:33:56.814 I/mono-stdout( 8974): "last_updated_by": 1, 02-14 16:33:56.815 I/mono-stdout( 8974): "meta_keyword_ar": "", 02-14 16:33:56.815 I/mono-stdout( 8974): "meta_keyword_en": "", 02-14 16:33:56.815 I/mono-stdout( 8974): "order_by": 94, 02-14 16:33:56.815 I/mono-stdout( 8974): "sub_category_ar": "Trailers", 02-14 16:33:56.815 I/mono-stdout( 8974): "sub_category_en": "Trailers", 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_name_parents_ar": null, 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_name_parents_en": null, 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_url": "trailers", 02-14 16:33:56.816 I/mono-stdout( 8974): "sub_category_url_parents": null 02-14 16:33:56.816 I/mono-stdout( 8974): }, 02-14 16:33:56.817 I/mono-stdout( 8974): { 02-14 16:33:56.817 I/mono-stdout( 8974): "active": 1, 02-14 16:33:56.817 I/mono-stdout( 8974): "child_level": 1, 02-14 16:33:56.817 I/mono-stdout( 8974): "create_date": "2015-08-27T17:25:14.31+03:00", 02-14 16:33:56.817 I/mono-stdout( 8974): "description_ar": "", 02-14 16:33:56.818 I/mono-stdout( 8974): "description_en": "", 02-14 16:33:56.818 I/mono-stdout( 8974): "has_child": 1, 02-14 16:33:56.818 I/mono-stdout( 8974): "id": 1882, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_category": 3, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_parent": 0, 02-14 16:33:56.818 I/mono-stdout( 8974): "id_parents": "0", 02-14 16:33:56.819 I/mono-stdout( 8974): "id_user": 1, 02-14 16:33:56.819 I/mono-stdout( 8974): "last_update": "2015-08-27T17:25:14.31+03:00", 02-14 16:33:56.819 I/mono-stdout( 8974): "last_updated_by": 1, 02-14 16:33:56.819 I/mono-stdout( 8974): "meta_keyword_ar": "", 02-14 16:33:56.819 I/mono-stdout( 8974): "meta_keyword_en": "", 02-14 16:33:56.820 I/mono-stdout( 8974): "order_by": 95, 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_ar": "Aeroplanes", 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_en": "Aeroplanes", 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_name_parents_ar": null, 02-14 16:33:56.820 I/mono-stdout( 8974): "sub_category_name_parents_en": null, 02-14 16:33:56.821 I/mono-stdout( 8974): "sub_category_url": "aeroplanes", 02-14 16:33:56.821 I/mono-stdout( 8974): "sub_category_url_parents": null 02-14 16:33:56.821 I/mono-stdout( 8974): } 02-14 16:33:56.821 I/mono-stdout( 8974): ]

我在 Visual Studio 2013 中使用了 xamarin。感谢您的帮助。如果使用这个 Newtonsoft.Json.JsonSerializer 是我的问题。我可以有一个小的示例代码吗?谢谢

0 投票
1 回答
5955 浏览

c# - 为什么 System.Net.Http HttpClient 对我的请求 URL 进行编码?

我使用 System.Net.Http 中的 HttpClient 向 Web 服务发出请求,如下所示:

我有一个沙盒应用程序和一个实时应用程序。沙盒应用程序具有相同的代码(在共享存储库中),可以正常工作,但是当client.GetAsync(url).Result在实时应用程序中调用时,由于某种原因,Fiddler 向我显示请求的 URL 已被编码,这会弄乱请求。

请求的 URL 应该如下所示:

但最终看起来像这样:

知道为什么吗?谢谢

注意我在 .NET Framework 4.5 中使用来自 Nuget 的 Microsoft.Net.Http 库

0 投票
0 回答
444 浏览

c# - HttpWebRequest 在第二次调用时得到相同的响应对象

我正在使用 HttpWebRequest 通过 POST 方法获取网页源代码。该页面需要使用不同的参数多次访问。

在短时间内第二次获得响应时,它总是向我返回相同的响应对象。经过调试,如果第二次调用是在 30 秒左右之后,请求可以得到正确的响应对象(源代码)。

函数调用块:

我一整天都在纠结这个问题。希望有人能给我一个线索。

非常感谢!

0 投票
1 回答
159 浏览

c# - 国际字符编码

我正在制作一个 Google 建议 API

我正在请求它应该返回的“¿cómo estás”词

¿cómo estás ¿cómo estás hoy ¿cómo estás 意思 ¿cómo estás tú ¿cómo estás cuando nadas ¿cómo estás yo (1) ¿cómo estás yo ¿cómo estás 回复 ¿cómo estás 英文

但它显示

[{"Phrase":"como estás"},{"Phrase":"cómo estás en inglés"},{"Phrase":"como estás em espanhol"},{"Phrase":"cómo estás lleva acento"} ,{"Phrase":"cómo estás tú"},{"Phrase":"cómo estás hola"},{"Phrase":"como estas mean"},{"Phrase":"como estas reply"},{ "短语":"como estas usted"},{"短语":"como estas hoy"}]

响应是

在此处输入图像描述

忽略关键字。

你可以看到

¿

结果中缺少

我的代码是

请告知应该使用哪种编码...

0 投票
0 回答
783 浏览

asp.net-web-api - 使用 .NET 4.0 和 HttpClient 上传大文件 (~600MB) 时出现 System.OutOfMemoryException

我只在我的本地工作站和产品服务器上收到此错误。在 Dev 和 Cert 中它工作正常。

本地工作站 - 20 GB 内存,Win 7 64 位,IIS Express,VS 2013
dev,cert & prod - 8 GB 内存,2008 R2 64 位,IIS 7.5

我有一个 web api (.net 4.0),它接受传入的请求正文并将其上传到存储服务器。根据此网站配置 web api 。

我的 web.config 中有这些

我还有一个IHostBufferPolicySelector的实现,它为 PUT 和 POST 请求返回 false。所以对这个 web api 的 PUt & POST 请求没有缓冲。

对于任何 < ~350 MB 的文件,它都可以正常工作。但是当文件大小 >= ~ 400 MB 时,web api 会抛出内存不足异常,并且这只发生在本地工作站和产品服务器上。

Web Api 控制器调用下面的代码将请求流式传输到目标服务器

在网上做了一些研究之后,我从这个链接这个链接了解到.Net 4.0 上的 HttpClient 缓冲了请求正文,并且由于这种行为,在我看来它正在抛出内存不足异常

所以我这次使用HttpWebRequest将我的代码更改为下面的代码,我可以使用它来控制指定请求应该被流式传输但不被缓冲。

现在它在我的本地机器上运行良好。我能够上传大约 1GB 的文件,而不会出现任何错误或内存异常。尚未将此推送到 Prod。

但是我仍然不明白为什么在 .net 4.0 上使用 HttpClient 的相同代码可以在 Dev 和 Cert 服务器上运行,但不能在 Prod 和我的本地服务器上运行。

请帮助我理解

如何找出它为什么在 Dev 和 Cert 上起作用?
哪些系统/服务器配置会影响此 api 的内存分配?

0 投票
1 回答
344 浏览

c# - How does HttpWebRequest cache responses? And why recycling app pool did not work?

What is the way of HttpWebRequest cache works?

I tried recycling the app pool on IIS in which my web application is hosted but I was still getting cached responses.

So cache entries are not stored in memory? How exactly it works?

0 投票
1 回答
457 浏览

http-post - Xamarin 表单 - 无法设置 HttpWebRequest 的 ContentLenght

我试图用 GET 和参数创建一个请求。但是,我遇到了 WinPhone8.1 的异常,这意味着由于添加了内容,GET 是违反协议的。因此,发出 POST 请求是解决方案。

尽管我进行了搜索,但我仍然无法设置 HttpWebRequest 的内容长度属性。为什么?