Making an HTTP POST request in C# with UTF32?

2019-09-21 17:14发布

问题:

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();
    }

}

回答1:

"at least not in my scenario where UTF32 is needed"

UTF-32 isn't needed. You can express any Unicode code point using UTF-8.

The server you are talking to likely doesn't support UTF-32, and that's the whole problem.

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).



回答2:

With you HttpWebRequest object, do as follows:

var req = WebRequest.Create("http://posturl") as HttpWebRequest;
var encodedBytes = System.Text.Encoding.UTF32.GetBytes("your string of data");
using (var stream = req.GetRequestStream())
   stream.Write(encodedBytes, 0, encodedBytes.Length);


标签: c# http post