I want to make a HTTP POST request in C# with UTF32. I can find plenty of resources on this with UTF8, but none of them appear to work properly - at least not in my scenario where UTF32 is needed.
Can you help me?
Edit 1
The code is located here:
public static void SubmitMap(string mapPath)
{
var request = WebRequest.Create(Domain + "/MapCloud/SubmitMap");
request.Method = "POST";
var postData = "facebookID=" + IngamableCommunicator.FacebookProfileID + "&name=Sample&content=" + /*HttpUtility.UrlEncode(File.ReadAllText(mapPath)*/ "lala";
var byteArray = Encoding.UTF32.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
var dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
var response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
response.GetResponseStream();
//dataStream = response.GetResponseStream();
//StreamReader reader = new StreamReader(dataStream);
//string responseFromServer = reader.ReadToEnd();
//Console.WriteLine(responseFromServer);
//reader.Close();
dataStream.Close();
response.Close();
}
The code returns an error 500. Of course, this could be an issue with the server. But the server is configured to output a stacktrace as a string in case of an error.
Edit 2
I tried changing the content type to "text/plain" which it basically is. Still no luck.
Edit 3
The server is running ASP .NET MVC 3 on the .NET Framework 4.0, and the controller for the area that is contacted looks like this:
public class MapCloudController : Controller
{
[HttpPost]
public ActionResult SubmitMap(string name, string content, int facebookID)
{
try
{
var container = new ModelContainer();
Gamer gamer = container.GamerSet.FirstOrDefault(g => g.FacebookID == facebookID);
var map = new ScaveniusMap();
map.Content = content;
map.Name = name;
map.SubmissionTime = DateTime.UtcNow;
map.Owner = gamer;
container.AddToScaveniusMapSet(map);
Debug.Assert(gamer != null, "gamer != null");
gamer.Maps.Add(map);
container.SaveChanges();
container.Dispose();
ViewBag.Error = "No error";
}
catch (Exception ex)
{
ViewBag.Error = facebookID + ": " + ex;
}
Response.StatusCode = 200;
Response.Status = "success";
Response.SubStatusCode = 0;
return View();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…