2013-11-15 24 views
4

我想發送一個HttpWebRequest一些數據在希臘我的電子商店使用C#。問題是在電子商店希臘字符。看起來像???????? ??????? 150?250HttpWebRequest通過C#和UTF-8字符集

我的c#代碼是在波紋管。如果有人知道如何解決這個問題,請幫忙。

   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(); 
       } 
+1

你編碼爲ASCII希臘字母,而不是UTF8 。 –

+0

你是對的人。謝謝。我看到這只是發佈問題修復它,然後你回答。非常感謝!它現在工作正常! –

回答

9

您可以使用

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

這種編碼支持特殊字符,如不包含在ASCII相當於

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

謝謝你的回答 –