【发布时间】:2011-04-14 15:23:44
【问题描述】:
我正在用 C# 编写一个与 API 连接的小型应用程序。
我连接到一个 API,该 API 有一个方法,该方法需要一个长字符串,即日历(ics)文件的内容。
我是这样做的:
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。
如果我的日历(或类似)中某处有“ ”,则服务器只会获取“&”点之前的所有数据,所以我假设“&”使它看起来像在此之后的任何东西点属于新参数?
我该如何解决这个问题?
【问题讨论】:
-
在发送之前对字符串进行编码?
标签: c# post httpwebrequest