Method Post on Code Behide with asp.net (แก้ปัญหาภาษาไทย)
จากเนื้อหาเก่า Method Post on Code Behide with asp.net มีปัญหาในการส่งข้อมูลประเภทภาษาไทย ดังนั้นในส่วนของการ encode ให้ทำแบบนี้ วิธีนี้ได้มาจาก krucode.net ครับ
1 2 | System.Text.Encoding encoder= System.Text.Encoding.GetEncoding(65001); byte[] byteData = encoder.GetBytes(outputBuffer); |
เมื่อเอามารวมกันเต็มๆก็จะเป็นแบบโค๊ดด้านล่างครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 | string responseString = ""; string outputBuffer = string.Format("msg={0}&senderemail={1}&sendername={2}&tel={3}",mail.Msg, mail.SenderEmail, mail.SenderName, mail.Tel); WebClient postWebClient = new WebClient(); postWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); string postUrl = "http://localhost:53680/aspgod/contact_receive.aspx"; // encode System.Text.Encoding encoder= System.Text.Encoding.GetEncoding(65001); byte[] byteData = encoder.GetBytes(outputBuffer); byte[] responseArray = postWebClient.UploadData(postUrl, "POST", byteData); responseString = System.Text.Encoding.ASCII.GetString(responseArray); Response.Write(responseString); |
สำหรับคนที่ส่งสัยว่าทำไมต้องเป็นเลข 65001 ให้ตามมาดูที่ System.Text.Encoding.GetEncoding(WhatValidStrings) ได้เลยครับ