StreamWriter ile Türkçe karakterleri yazabilmek

Sanırım birçok arkadaşımızın ihtiyacı olacak bir bilgidir. Hatta Eylül arkadaşımız şurada sormuş, cevap vermeyi atlamışım:

string result = string.Empty;
HttpWebRequest m_WebRequest;
HttpWebResponse m_WebResponse;
Encoding m_Encoding;
byte[] m_PostData;
Stream m_WriteStream;
Stream m_ResponseStream;
StreamReader m_StreamReader;

m_Encoding = Encoding.GetEncoding("iso-8859-9");
m_PostData = m_Encoding.GetBytes("ığüşiöç");
m_WebRequest = HttpWebRequest.Create("http://sunali.com") as HttpWebRequest;

m_WebRequest.Method = "POST";
m_WebRequest.ContentLength = m_PostData.Length;
m_WebRequest.ContentType = "application/x-www-form-urlencoded";

try
{
    m_WriteStream = m_WebRequest.GetRequestStream();
}
catch (System.Net.WebException)
{
    throw;
}

m_WriteStream.Write(m_PostData, 0, m_PostData.Length);
m_WriteStream.Close();
m_WriteStream.Dispose();

m_WebResponse = m_WebRequest.GetResponse() as HttpWebResponse;
m_ResponseStream = m_WebResponse.GetResponseStream();
m_StreamReader = new StreamReader(m_ResponseStream, m_Encoding);

result = m_StreamReader.ReadToEnd();

m_ResponseStream.Close();
m_ResponseStream.Dispose();
m_WebResponse.Close();
m_WebRequest.Abort();

Response.Write(result);

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *