问题描述
我想在C#中使用UTF32发出HTTP POST请求。我可以通过UTF8找到大量资源,但是它们都没有正常工作 - 至少在我需要UTF32的情况下不行。
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.
你能帮助我吗? ?
编辑1
代码位于:
Edit 1The 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();
}
代码返回错误500.当然,这可能是服务器的问题。但服务器配置为在出现错误时将堆栈跟踪输出为字符串。
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.
编辑2
我尝试更改内容类型为text / plain,它基本上是。仍然没有运气。
Edit 2I tried changing the content type to "text/plain" which it basically is. Still no luck.
编辑3
服务器在.NET Framework 4.0上运行ASP .NET MVC 3,并且联系区域的控制器如下所示:
Edit 3The 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();
}
}
推荐答案
至少在我需要UTF32的场景中
"at least not in my scenario where UTF32 is needed"
不需要UTF-32。您可以使用UTF-8表达任何Unicode代码点。
UTF-32 isn't needed. You can express any Unicode code point using UTF-8.
您正在与之交谈的服务器可能不支持UTF-32,这就是整个问题。
The server you are talking to likely doesn't support UTF-32, and that's the whole problem.
此外,如果您使用application / x-www-form-urlencoded,您甚至可以选择字符编码。相反,您必须对那些不是ASCII的字符进行百分比编码(请参阅定义此媒体类型的HTML4规范)。
Also, if you use "application/x-www-form-urlencoded", you may even have the option to select the character encoding. Instead, you'll have to percent-encode those characters that are not ASCII (see HTML4 spec which defines this media type).
这篇关于使用UTF32在C#中发出HTTP POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!