Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Making an HTTP POST request in C# with 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...