1

I've a code block like this:

        const string webServiceUrl = "https://test.xyxyx.com/App/Services/wService.svc?wsdl";
        var postString = string.Format("Parameter1={0}&Parameter2={1}&Parameter3={2}&Parameter4={3}&Parameter5={4}&Parameter6={5}&Parameter7={6}&Parameter8={7}", "AA", "AB", "AC", "BA", "BB", BC, 5, 7);

        const string contentType = "text/xml; charset=utf-8";
        HttpWebRequest webRequest = WebRequest.Create(webServiceUrl) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = contentType;
        webRequest.Accept = "text/xml";

        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postString);
        requestWriter.Close();

        StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
        var responseData = responseReader.ReadToEnd();
        responseReader.Close();
        webRequest.GetResponse().Close();

I get the error on this line:

StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

The remote server returned an error: (400) Bad Request

Why I get this? I tried some different methods, but result is the same. What can I do?

1teamsah
  • 1,863
  • 3
  • 23
  • 43

1 Answers1

0

I think the problem is that you're posting to ?wsdl url.

Also, your content is not xml.

Generally, you can examine the content of the returned page, it may have some more details.

Finally check out .NET: Simplest way to send POST with data and read response .

tymtam
  • 31,798
  • 8
  • 86
  • 126
  • I get this: Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'. how can I set the content-type by 'text/xml; charset=utf-8' for this method? – 1teamsah Oct 26 '17 at 06:28