简体   繁体   English

如何设置.Net WebClient :: UploadString()的内容类型?

[英]How to set content-type of .Net WebClient::UploadString()?

I am using .net in C++. 我在C ++中使用.net。

How to set http header content-type if using .Net WebClient::UploadString() or WebClient::UploadData() ? 如果使用.Net WebClient::UploadString()WebClient::UploadData()如何设置HTTP标头内容类型?

Basically, I want to send some Unicode string to the HTTP server (using Mongoose), and the server will then decode the data received base on the content-type field. 基本上,我想将一些Unicode字符串发送到HTTP服务器(使用Mongoose),然后服务器将根据content-type字段对接收到的数据进行解码。 Is there another alternative or any better solution? 还有其他选择或更好的解决方案吗?

Thanks 谢谢

There is a Headers member as part of the WebClient class. WebClient类中有一个Headers成员。 You can set the Content-Type header by adding it to the Headers collection: 您可以通过将Content-Type标头添加到Headers集合来设置它:

myWebClient->Headers->Add("Content-Type","application/x-www-form-urlencoded");

Subsquent calls to UploadString (or other methods that make requests) the header will be sent along with the request. 对标头的UploadString(或其他发出请求的方法)的后续调用将与请求一起发送。

Call WebClient::GetWebRequest() —it returns an HttpWebRequest object with a ContentType property. 调用WebClient::GetWebRequest() -它返回具有ContentType属性的HttpWebRequest对象。 You then pass it to WebClient::GetWebResponse() to send the request. 然后,将其传递给WebClient::GetWebResponse()发送请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM