我正在从基于c#console的应用程序向我的API发送一些日文字符,但我没有像
那样正确地使用它我发送 - 配赋率登录 通过此代码编码后 -
HttpUtility.UrlEncode(entry.Value, Encoding.UTF8);
该值变为 -
%E9%85%8D%E8%B3%A6%E7%8E%87%E7%99%BB%E9%8C%B2
这是一个正确的UTF-8编码值,但在API端我应该
%e9%85%8d%e8%b3%a6%e7%8e%87%e7%99%bb%e9%8c%b2,
但我得到了 -
éè³|ç»»é²
意外的价值。
//这是我的代码
public string CallRestMethodPost(string apiUrl, string data1)
{
File.WriteAllBytes("C:\\file.txt", Encoding.UTF8.GetBytes("こんにちは"));
// Create a request using a URL that can receive a post.
// data1 = "project_id=278&screen_name=01&english_uispecs_name=01_uispecs_english.xls&japanese_uispecs_name=01_uispecs_japanese.xls&function_id=dsdas&subsystem=配賦率登録";
WebRequest request = WebRequest.Create(apiUrl);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
// string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(data1);
File.WriteAllBytes("C:\\fileq.txt", byteArray);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
答案 0 :(得分:3)
问题在于内容类型
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
答案 1 :(得分:0)
您可以在发送之前尝试Base64对数据进行编码。