I'm writing a small API-connected application in C#.
I connect to a API which has a method that takes a long string, the contents of a calendar(ics) file.
I'm doing it like this:
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();
}
This seems to work great, until I add some specific HTML in my calendar.
If I have a ' ' 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?
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.
Try passing your string through HttpUtility.UrlEncode before writing it to the request stream.
Here are a couple links for reference.