Question

I'm trying to call http://genderize.io/ , but i'm getting an error from .NET saying:

{"You must provide a request body if you set ContentLength>0 or SendChunked==true.  Do this by calling [Begin]GetRequestStream before [Begin]GetResponse."}

How would I call this web service "http://api.genderize.io/?name=peter" from C# and get a JSON string back?

HttpWebRequest request;
                string postData = "name=peter"
    URL = "http://api.genderize.io/?"
                Uri uri = new Uri(URL + postData);
                request = (HttpWebRequest)WebRequest.Create(uri);

                request.Method = "POST";

                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;

                request.AllowAutoRedirect = true;

                UTF8Encoding enc = new UTF8Encoding();

                string result = string.Empty;

                HttpWebResponse Response;
                try
                {
                    using (Response = (HttpWebResponse)request.GetResponse())
                    {
                        using (Stream responseStream = Response.GetResponseStream())
                        {
                            using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                            {
                                return readStream.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error: " + ex.Message);
                    throw ex;
                }
Était-ce utile?

La solution

You are making the call to the service using POST method, reading through the comments area in http://genderize.io/ the author states that only GET method requests are allowed.

Stroemgren: Yes, this is confirmed. Only HTTP GET request are allowed.


This answer probably would be better as a comment, but I don't have enough reputation :(

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top