C#类型&QUOT的HttpWebRequest的;应用程序/ x-WWW的形式urlen codeD" - 如何发送'和;'字符内容的身体吗? [英] C# HttpWebRequest of type "application/x-www-form-urlencoded" - how to send '&' character in content body?

查看:91
本文介绍了C#类型&QUOT的HttpWebRequest的;应用程序/ x-WWW的形式urlen codeD" - 如何发送'和;'字符内容的身体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中一个很小的API连接应用程序。

I'm writing a small API-connected application in C#.

我连接到其具有需要很长的串,日历(ICS)文件的内容的方法的API。

I connect to a API which has a method that takes a long string, the contents of a calendar(ics) file.

我做的是这样的:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.AllowAutoRedirect = false;
request.CookieContainer = my_cookie_container;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";

string iCalStr = GetCalendarAsString();

string strNew = "&uploadfile=true&file=" + iCalStr;

using (StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII))
 {
     stOut.Write(strNew);
     stOut.Close();
 }

这似乎工作的伟大,直到我在我的日历中添加一些特定的HTML。

This seems to work great, until I add some specific HTML in my calendar.

如果我有一个'和; NBSP在我的日历(或类似)服务器只获取的所有数据到某个地方'和;' - 点,所以我假设'和;'使它看起来像什么这点属于一个新的参数后?

If I have a '&nbsp' somewhere in my calendar (or similar) the server only gets all the data up to the '&'-point, so I'm assuming the '&' makes it look like anything after this point belongs to a new parameter?

我该如何解决这个问题?

How can I fix this?

推荐答案

由于您的内容类型为应用程序/ x-WWW的形式urlen codeD 你马上需要连接code中的POST体,尤其是如果它包含像&放个字符; 这有一个形式特殊的意义。

Since your content-type is application/x-www-form-urlencoded you'll need to encode the POST body, especially if it contains characters like & which have special meaning in a form.

试用写入之前,通过你的字符串,通过 HttpUtility.UrlEn code 请求流。

Try passing your string through HttpUtility.UrlEncode before writing it to the request stream.

下面是引用几个链接。

这篇关于C#类型&QUOT的HttpWebRequest的;应用程序/ x-WWW的形式urlen codeD" - 如何发送'和;'字符内容的身体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆