1

I am using the below code to post the request with multipart/form-data content type but got the exception:

The remote server returned an error: (532).

How I can solve this?

public void request222(string cgid)
{
    NameValueCollection nvc = new NameValueCollection();
    nvc.Add("action:WebManager", "OK");
    nvc.Add("cg_id", "" + cgid + "");

    var boundary = "---------------------------DateTime.Now.Ticks.ToString("x")";

    //creating request
    var wr = (HttpWebRequest)WebRequest.Create("http://189.126.121.79:8093/API/CCG");
    wr.ContentType = "multipart/form-data; boundary=" + boundary;
    wr.Method = "POST";
    wr.KeepAlive = true;

    //sending request
    using (var requestStream = wr.GetRequestStream())
    {
        using (var requestWriter = new StreamWriter(requestStream, Encoding.UTF8))
        {
            //params
            const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
            foreach (string key in nvc.Keys)
            {
                requestWriter.Write(boundary);

                requestWriter.Write(String.Format(formdataTemplate, key, nvc[key]));
            }
            requestWriter.Write("\r\n--" + boundary + "--\r\n");
        }
    }

    //reading response
    try
    {

        using (var wresp = (HttpWebResponse)wr.GetResponse())
        {
            if (wresp.StatusCode == HttpStatusCode.OK)
            {
                using (var responseStream = wresp.GetResponseStream())
                {
                    if (responseStream == null)

                    using (var responseReader = new StreamReader(responseStream))
                    {
                        string s= responseReader.ReadToEnd();
                    }
                }
            }

            throw new ApplicationException("Error Server status code: " + wresp.StatusCode.ToString());
        }
    }
    catch (Exception ex)
    {
        throw new ApplicationException("Error while uploading file", ex);
    }
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
suri
  • 37
  • 1
  • 9
  • That's a server error, so your request is probably invalid. Whether that's because it's semantically wrong HTTP or a wrong request message according to the API is something you can figure out. – CodeCaster Sep 05 '15 at 13:24

1 Answers1

0

Rather then implementing it on your own, consider using the new API instead: the HttpClient class. It has support for multipart/form-data.

For a practical example, see e.g. this answer on another question

Also, it might be better to just use application/x-www-form-urlencoded instead, since you don't have any files posted in your request (at least in the example you provided)

Community
  • 1
  • 1
torvin
  • 6,515
  • 1
  • 37
  • 52
  • Tried one Http client but got the connection error: HttpClient c = new HttpClient(); var formData = new FormUrlEncodedContent(new[] { new KeyValuePair("action:WebManager", "OK"), new KeyValuePair("cg_id", cgid) }); MultipartContent content = new MultipartContent(); content.Add(formData); var result = c.PostAsync("http://189.126.121.79:8093/API/CCG", content).Result; – suri Sep 05 '15 at 18:44
  • You should not mix `FormUrlEncodedContent` with `MultipartContent`. See example here: http://stackoverflow.com/a/15176685/332528 – torvin Sep 05 '15 at 23:27