안녕하세요.
아래의 소스에서는 http://www.khan.co.kr 은 euc-kr 를 사용한 html 인데도 자동으로 utf-8로 변환시켜주고 있는 반면에
http://m.dt.co.kr 에서 받은 한글은 변환을 못시킵니다. 원인이 무엇인지 모르겠습니다.
소스코드가 아래와 같을 경우 euc-kr 를 utf-8로 변환시키는 가장 간결한 방법이 있을까요?
ApiController
using System.Web.Http;
using System.Threading.Tasks;
using System.Net.Http;
using System;
namespace Test.Controllers
{
public class TestController : ApiController
{
public class GetValue
{
public string Url { get; set; }
}
static readonly HttpClient client = new HttpClient();
[HttpPost]
public async Task<string> PostAsync(GetValue p)
{
String AnsiString ;
String UTF8String;
HttpResponseMessage response = await client.GetAsync(p.Url);
response.EnsureSuccessStatusCode();
AnsiString = await response.Content.ReadAsStringAsync();
UTF8String = AnsiString;
return UTF8String;
}
}
}
Html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-3.3.1.js" ></script>
<title></title>
</head>
<body>
<br />
<br />
<br />
<p> http://m.dt.co.kr ( 한글깨짐 ) </p>
<p>http://www.khan.co.kr ( EUC-KR 인데 한글이 깨지지 않는다 ) </p>
<hr />
<input type="text" id="url" />
<button type="button" xxonclick="euckrurl()"> 전송 </button>
<hr />
<pre id="content"></pre>
<script>
function euckrurl() {
var url = document.getElementById("url").value;
var ApiValue = new Object();
ApiValue.Url = url;
$.ajax({
url: '/api/Test',
type: 'POST',
dataType: 'json',
data: ApiValue,
success: function (data, textStatus, xhr) {
document.getElementById("content").innerText = data;
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
}
</script>
</body>
</html>
첫댓글 var contentType = 'application/json; charset=utf-8';
$.ajax({
url: "",
type: "post",
dataType: "json",
contentType: contentType,
저는 잘 몰라 옆에 물어보고 알려드립니다. 문법이 맞는지는 모르겠네요
안됩니다. 여러 글들을 검색해 보았더니 알려주신 코드가 정답인 것 같다는 느낌은 드는데 안되는 군요. 뭔가 빠진것 같습니다.
그런데 php 에서는 받은 데이터를 분석해서 EUC-KR이면 utf-8로 바꿔주는 함수가 있는 것 같습니다. 누군가 한 사람이 함수를 만들어놓은 것을 공개하면 다른 php 하는 사람들이 쓰는 것 같습니다.
닷넷에서도 HttpResponseMessage 로 받은 데이타가 EUC-KR이면 utf-8로 바꾸는 함수를 만들어서 쓰고 있는 사람이 있지 싶은데 공개를 하지 않는 듯합니다.
web.config쪽에 어떤 설정이 있었던것 같긴한뎅.. 웹한지 오래되서..
https://webisfree.com/2018-05-25/asp-net-%ED%95%9C%EA%B8%80-%EA%B9%A8%EC%A7%90-%ED%98%84%EC%83%81-%EC%9D%B8%EC%BD%94%EB%94%A9-%ED%95%B4%EA%B2%B0%EB%B0%A9%EB%B2%95
알려주신 방법도 안되는군요. 카키104님과 퇴근5분전님께서 알려주신 방법 둘 다 상식적으로 생각해서는 되야하는 방법인 것인데요. 왜 안될까요?