简体   繁体   中英

HttpWebRequest via C# and UTF-8 charset

I am trying to send a HttpWebRequest some data in Greek to my e-shop using c#. The problem is that in e-shop the greek chars. looks like ???????? ??????? 150?250 ???????? ??????? 150?250

My c# code is in bellow. If some knows how can fix this please help.

                EShopProduct eshopProduct = new EShopProduct();
                eshopProduct = GetEShopProductByProductCode(reference);

                string scode = eshopProduct.SCode;
                string name = eshopProduct.Name;
                string description = eshopProduct.Description;
                string quantity = eshopProduct.Quantity;
                string category = eshopCategoryId;
                string price = eshopProduct.Price;

                string postData = String.Format("scode={0}&name={1}&description={2}&quantity={3}&category={4}&price={5}&reference={6}",
                                                 scode, name, description, quantity, category, price, reference);

                string getUrl = _eshopUrl + "/insert_a_product_bysgs.php";

                HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
                getRequest.Method = WebRequestMethods.Http.Post;
                getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; // SGS Galaxy
                getRequest.AllowWriteStreamBuffering = true;
                getRequest.ProtocolVersion = HttpVersion.Version11;
                getRequest.AllowAutoRedirect = true;
                getRequest.ContentType = "application/x-www-form-urlencoded";

                byte[] byteArray = Encoding.ASCII.GetBytes(postData);
                getRequest.ContentLength = byteArray.Length;
                Stream newStream = getRequest.GetRequestStream(); //open connection
                newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
                newStream.Close();

                HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
                using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) {
                    new_product_id = sr.ReadToEnd();
                }

You can use

byte[] byteArray = Encoding.UTF8.GetBytes(postData);

This encoding supports special characters such as Greek letters that are not included in the ASCII equivalent

byte[] byteArray = Encoding.ASCII.GetBytes(postData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM