views:

160

answers:

0

I'm making an http post to an url.

When using WebClient or WebRequest it all works fine, unless I change the WebRequest.ContentType propertie.

What I'm missing here?

As suggested here I'm trying this:

using (WebClient client = new WebClient())
    {
        NameValueCollection fields = new NameValueCollection();
        fields.Add("query", query);
        client.UploadValues(url, fields);
        byte[] respBytes = client.UploadValues(url, fields);
        string resp = client.Encoding.GetString(respBytes);
    }

And this (as suggested here)

            Uri uri = new Uri(url);
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);

            request = (HttpWebRequest)WebRequest.Create(uri);

            request.Credentials = new NetworkCredential("user", "pass");
            request.PreAuthenticate = true;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            request.ContentLength = postData.Length;
            using (Stream writeStream = request.GetRequestStream())
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(postData);
                writeStream.Write(bytes, 0, bytes.Length);
            }

Being postData a NameValueCollection.