Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

StreamWritter UTF8 Encoding Error in database class

$
0
0

Hi,

We are developing an android, wp8 &iOS application using File Linking strategy of Xamarin. We have written WCF REST Service to input/output as JSON format data.

In database class we have used ASCII format and it works fine, but we need to support multiple languages, so we tried to change UTF8 format. While changing we are getting error as "The remote server returned an error: (400) Bad Request".

Please find the sample database function for sending data to WCF REST Service. Kindly suggest to resolve the same.

public async Task<String> GetString<T>(T item, String ServiceAddress, String FunctionName, String[] Parameters, object[] ParameterValues) where T : BusinessLayer.Contracts.IBusinessEntity, new()
        {
            string ParameterString = string.Empty;
            try
            { 
                var request = HttpWebRequest.Create(ServiceAddress);
                request.ContentType = "application/json";
                request.Method = "POST";
                request.Headers["Authorization"] = GlobalVariables.glbtoken;
                if (Parameters.Length > 0)
                    ParameterString = "{";
                for (int i = 0; i < Parameters.Length; i++)
                {
                    ParameterString = ParameterString + "\"" + Parameters[i] + "\":\"" + ParameterValues[i] + "\"" + ",";
                }
                if (Parameters.Length > 0)
                {
                    ParameterString = ParameterString.Remove(ParameterString.Length - 1, 1);
                    ParameterString += "}";
                }
                // var sw = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                 var sw = new StreamWriter(request.GetRequestStream(), Encoding.UTF8);
                sw.Write(ParameterString); //This string is not passed to server and shows null 
                sw.Close();
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) // Here returned as remote server returned error
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        var content = reader.ReadToEnd();                
                        if (content != "")
                            OutputString = (string)Newtonsoft.Json.JsonConvert.DeserializeObject(content);
                    }
                }
            }
            catch (Exception e) { }
            return OutputString;
        }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>