0

I have problem with getting response from Web Services. I tried to find solution here, but without success.

Can someone help me with this, please?

Iam getting Error:

Error: System.Net.WebException: The remote server returned an error: (400) Bad Rquest.

I needed to remove links, because I can not post this question with them.

Thanks in advance!

Code:

private void CallWebService() 
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(link);
            req.Referer = "link";
            req.UserAgent =
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; " +
                "Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; " +
                ".NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; " +
                "InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)";

            HttpWebRequest httpreq = (HttpWebRequest)req;
            httpreq.Method = "POST";
            httpreq.ContentType = "text/xml; charset=utf-8";
            req.ContentLength = 0;
            httpreq.Headers.Add("SOAPAction: link");
            StringBuilder soaprequest = new StringBuilder("<soap:Envelope xmlns:xsi=link"");
            soaprequest.Append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
            soaprequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
            soaprequest.Append("<getProductListDownloadXML xlmns=http://link>");
            soaprequest.Append("<login>**</login>");
            soaprequest.Append("<password>**</password>");
            soaprequest.Append("<encoding>utf-8</encoding>");
            soaprequest.Append("<onStock>false</onStock>");
            soaprequest.Append("</getProductListDownloadXML></soap:Body></soap:Envelope>");

            try
            {
                HttpWebResponse res = (HttpWebResponse)httpreq.GetResponse();
                string response = res.GetResponseStream().ToString();
                MessageBox.Show(response);
            }
            catch (WebException ex) 
            {
                string error = ex.ToString();
                if (!String.IsNullOrWhiteSpace(error)) 
                {
                    MessageBox.Show(error);
                }
            }
}
Mykola
  • 3,343
  • 6
  • 23
  • 39
  • Why your ContentLength=0? The envelope is content. ContentLength= Encoding.UTF8.GetBytes(soaprequest.ToString()) – vitalygolub Feb 18 '16 at 10:23
  • Hello, I can not convert byte to long – Jaroslav Šusta Feb 18 '16 at 11:13
  • Sorry, forgot to add .Length at the end. You really can,t convert byte array to int. But, unfortunately, your code has more problems, you forgot to append your content after gathered the envelope into stringbulder. You can copy code here http://stackoverflow.com/questions/4256136/setting-a-webrequests-body-data – vitalygolub Feb 22 '16 at 17:29
  • Hello, yeah I already noticed it. But I repaired it next day... Thank you anyway! – Jaroslav Šusta Mar 02 '16 at 18:58

0 Answers0