[英]HttpClient Post to ApacheServer (Debian)
I have ac# WPF application which sends a POST request to an php site. 我有一个ac#WPF应用程序,它将POST请求发送到php站点。 The site is hosted on an apache server which runs on Debian. 该站点托管在Debian上运行的apache服务器上。
My problem is sending chars like äöüß, those aren't encoded right i suppose because if i open the php site directly there aren't those chars but symbols. 我的问题是发送像äöüß这样的字符,我想这些字符没有正确编码,因为如果我直接打开php网站,则不是那些字符,而是符号。
I already tried setting charset to utf-8. 我已经尝试将字符集设置为utf-8。 Did i forget something? 我忘了什么吗?
Here's some code: 这是一些代码:
var url = "http://internalServer/input.php"
using (var client = await Login())
{
if (client == null) return false;
foreach (var item in day.Workitems)
{
if (item.IsSynced) continue;
try
{
var syncedText = item.Description;
if (!string.IsNullOrEmpty(item.Notes))
{
foreach (var line in item.Notes.Split(new [] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries))
{
if (!string.IsNullOrEmpty(syncedText)) syncedText += ", ";
syncedText += line;
}
}
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Ticket", item.Nr),
new KeyValuePair<string, string>("Text", syncedText)
});
await DoRequest(client, url, content);
}
catch (Exception)
{
return false;
}
}
}
return true;
}
private async Task<string> DoRequest(HttpClient client, string url, HttpContent content = null)
{
var request = new HttpRequestMessage(content == null ? HttpMethod.Get : HttpMethod.Post, url);
if (content != null)
{
request.Content = content;
}
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
In C#: try encoding with base64. 在C#中:尝试使用base64进行编码。 see .netfiddle 参见.netfiddle
using System;
public class Program
{
public static void Main()
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("äöüß");
string syncedText = Convert.ToBase64String(encbuff);
Console.WriteLine(syncedText); // you get "w6TDtsO8w58="
}
}
In PHP: enforcing utf-8 在PHP中:执行utf-8
<?php
$str = $_REQUEST['Text']; // "w6TDtsO8w58="
echo base64_decode($str); // you get "äöüß"
?>
If you don't want to change anything in C# side, then try this 如果您不想在C#端进行任何更改,请尝试此操作
<?php
// you may want to detect the encoding of the request string
$encoding = mb_detect_encoding($str,"UTF-8,ISO-8859-1,ISO-8859-2,windows-1250");
if( $encoding == "UTF-8" ) {
echo utf8_decode($str);
// do something ...
}
else {
// if encoded with charset windows-1250, ISO-8859-2, ISO-8859-1
// e.g, $str = "Zusammenf%FChrung"; // %FC = ü
echo mb_convert_encoding(urldecode($str), "utf-8", "ISO-8859-2");
// you get "Zusammenführung" as utf-8
}
?>
OR 要么
<?php
/*
* if your encoding is limited to a specific charset,
* you shall try changing php header as below.
*/
header('Content-Type: charset=ISO-8859-2');
echo urldecode("Zusammenf%FChrung");
// Note: insert the header() function before generating any content
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.