none
System.Net.WebException: The remote server returned an error: (400) Bad Request.

    Question

  • Hello all..

    I have builded a soap request.. but it's still not working, can some one help me out..?

    Error:
    System.Net.WebException: The remote server returned an error: (400) Bad Request. 
     
       at System.Net.HttpWebRequest.GetResponse() 
       at CallReportsConsole.MainClass.Main(String[] args) in C:\**\Console\Program.cs:line 57 
    The remote server returned an error: (400) Bad Request. 


    Code:
    using System; 
    using System.Net; 
    using System.IO; 
    using System.Text; 
    using System.Xml; 
     
    namespace CallReportsConsole 
     
        class MainClass 
        { 
             
     
            public static void Main(string[] args) 
            { 
                 
     
                try 
                { 
                    int username = 123456
                    string password = "wachtwoord"
     
                    //Create SOAP XML 
     
                    StringBuilder sbSoap = new StringBuilder(); 
     
                    //1. Build SOAP string 
                    sbSoap.Append("<? xml version=\"1.0\" encoding=\"utf-8\" ?>"); 
                    sbSoap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"); 
                    sbSoap.Append("<soap:Body>\n"); 
                    sbSoap.Append(" <GetProductSpecifications xmlns=\"http://www.bascomputers.nl/\">\n"); 
                    sbSoap.Append("<customerID>" + username + "</customerID>\n"); 
                    sbSoap.Append("<password>" + password + "</password>\n"); 
                    sbSoap.Append("</GetProductSpecifications>\n"); 
                    sbSoap.Append("</soap:Body></soap:Envelope>"); 
     
                    string soapAction = "http://www.bascomputers.nl/GetProductSpecifications"
     
                    //2. Send XML to CDR server 
                    string cdrURL = "http://xtract.bascomputers.nl:4040/xtract.asmx"
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL); 
     
                    req.ContentType = "text/xml; charset=utf-8"
                    req.Accept = "text/xml"
                    req.Method = "POST"
                    req.ContentLength = sbSoap.Length; 
                    req.Headers.Add("SOAPAction", soapAction); 
                    req.KeepAlive = false
     
                    Stream strm = req.GetRequestStream(); 
                    byte[] bytes = 
                    System.Text.Encoding.ASCII.GetBytes(sbSoap.ToString()); 
                    strm.Write(bytes, 0, bytes.Length); 
                    strm.Close(); 
     
                    //3. Get response & send back filename 
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
     
                    StreamReader r = new 
                    StreamReader(resp.GetResponseStream()); 
     
                    XmlTextReader xmlRead = new XmlTextReader(r); 
     
                    while (xmlRead.Read()) 
                    { 
                        Console.Write(xmlRead.Value.ToString()); 
                    } 
     
                    r.Close(); 
                    strm.Close(); 
     
                } 
     
                catch (WebException wex) 
                { 
                Console.Write(wex.ToString()); 
                Console.Write(wex.Message.ToString()); 
                } 
     
                catch (Exception ex) 
                { 
                Console.Write(ex.ToString()); 
                Console.Write(ex.Message.ToString()); 
                } 
                 
                Console.Read(); 
                 
            } 
        } 

    Monday, February 09, 2009 3:08 PM

Answers

  •  
    Use the following code.  

    try  
                {  
                    string soapRequest=  
                        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + "\n"  +  
                        "<soap:Body>  " + "\n" +  
                        "<m:GetProductSpecifications xmlns:m='http://www.bascomputers.nl/'>"  + "\n" +  
                        "<m:customerID>123456</m:customerID>"+ "\n" +  
                        "<m:password>wachtwoord</m:password>" + "\n" +  
                        "</m:GetProductSpecifications>"   + "\n" +  
                        "</soap:Body>"   + "\n" +  
                        "</soap:Envelope>" + "\n" ;  
     
                        
                    string soapAction = "http://www.bascomputers.nl/GetProductSpecifications";  
     
                    //2. Send XML to CDR server     
                    string cdrURL = "http://xtract.bascomputers.nl:4040/xtract.asmx";  
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL);  
     
                    req.ContentType = "text/xml; charset=utf-8";  
                    req.Accept = "text/xml";  
                    req.Method = "POST";  
                    //req.ContentLength = sbSoap.Length;  
                    req.Headers.Add("SOAPAction", soapAction);  
                    req.KeepAlive = false;  
                   XmlDocument doc = new XmlDocument();    
                    doc.LoadXml(soapRequest);    
                    using (Stream strm = req.GetRequestStream())  
                    {  
                        doc.Save(strm);  
                    }  
                    //XmlDocument doc = new XmlDocument();    
                    //  doc.LoadXML(soapRequest);    
                        
     
                    //3. Get response & send back filename     
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();  
     
                    //StreamReader r = new 
                    //StreamReader(resp.GetResponseStream());  
                    using (Stream respStm = resp.GetResponseStream())  
                    {  
                        using (StreamReader r = new StreamReader(respStm))  
                        {  
                              
                                string str = r.ReadToEnd();  
                             
                        }  
                    }  
                    
                } 

    Be Happy
    • Marked as answer by MartienoS Tuesday, February 10, 2009 12:51 PM
    Tuesday, February 10, 2009 12:35 PM

All replies

  • Hi, I think your soap message must be bad, can you post the wsdl containing the same method you are requesting for?
    Be Happy
    Monday, February 09, 2009 3:19 PM
  • POST /xtract.asmx HTTP/1.1 
    Host: xtract.bascomputers.nl 
    Content-Type: text/xml; charset=utf-8 
    Content-Length: length 
    SOAPAction: "http://www.bascomputers.nl/GetProductSpecifications" 
     
    <?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      <soap:Body> 
        <GetProductSpecifications xmlns="http://www.bascomputers.nl/"
          <customerID>int</customerID> 
          <password>string</password> 
        </GetProductSpecifications> 
      </soap:Body> 
    </soap:Envelope> 



    ______________________________________________________________________________________

    HTTP/1.1 200 OK 
    Content-Type: text/xml; charset=utf-8 
    Content-Length: length 
     
    <?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      <soap:Body> 
        <GetProductSpecificationsResponse xmlns="http://www.bascomputers.nl/"
          <GetProductSpecificationsResult>xml</GetProductSpecificationsResult> 
        </GetProductSpecificationsResponse> 
      </soap:Body> 
    </soap:Envelope> 

    ______________________________________________________________________________________

    The soap message is being used a million times, so that cant be wrong :)
    Link: http://xtract.bascomputers.nl:4040/xtract.asmx?op=GetProductSpecifications

    What do you meen with:
    "can you post the wsdl containing the same method you are requesting for?"
    Monday, February 09, 2009 3:28 PM
  • Hello I have tried from my side

    For the following SOAP request.
     request.ContentType = "text/xml;charset=\"utf-8\"";  
    request.Accept = "text/xml";  
    request.Method = "POST";  
     request.Headers.Add("SOAPAction","http://www.bascomputers.nl/GetProductSpecifications")
    <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>  
      <soap:Body> 
        <m:GetProductSpecifications xmlns:m="http://www.bascomputers.nl/">  
          <m:customerID>123456</m:customerID> 
          <m:password>wachtwoord</m:password> 
        </m:GetProductSpecifications> 
      </soap:Body> 
    </soap:Envelope> 
    I got the following response using the url and the credentials you have provided
      <soap:Body> 
        <GetProductSpecificationsResponse xmlns="http://www.bascomputers.nl/">  
          <GetProductSpecificationsResult> 
            <Products xmlns="">  
              <Error ID="1" Message="Object reference not set to an instance of an object.\" /> 
            </Products> 
          </GetProductSpecificationsResult> 
        </GetProductSpecificationsResponse> 
      </soap:Body> 
    </soap:Envelope> 
    can you try and lad the soap request to an xml document, then save it to the request stream.
    Somethign like this
    XMlDocument doc = new XMLDocument()
    doc.LoadXML(sb.Tostring())
    doc.Save(strm).

    In my case i am getting the response with an error from server side.
    Be Happy
    Tuesday, February 10, 2009 8:12 AM
  • thanks for replying me :) appreciated :)
    oke.. next.. ? i have added a XML file to the project.. i have imported the request like this:

    RequestXML.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      <soap:Body> 
        <GetProductSpecifications xmlns="http://www.bascomputers.nl/"
          <customerID>***</customerID> 
          <password>***</password> 
        </GetProductSpecifications> 
      </soap:Body> 
    </soap:Envelope> 


    in my form1.cs i have:



                StringBuilder sb = new StringBuilder();

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(sb.Tostring());
                doc.Save(strm);

    Error:

    Message    1    Could not find schema information for the element 'http://www.bascomputers.nl/:GetProductSpecifications'.    C:\**\TestRequest\RequestXML.xml    4    6    TestRequest 
    Message    2    Could not find schema information for the element 'http://www.bascomputers.nl/:customerID'.    C:\**\TestRequest\RequestXML.xml    5    8    TestRequest 
    Message    3    Could not find schema information for the element 'http://www.bascomputers.nl/:password'.    C:\**\TestRequest\RequestXML.xml    6    8    TestRequest 
    Error    4    'System.Text.StringBuilder' does not contain a definition for 'Tostring' and no extension method 'Tostring' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?)    C:\**\TestRequest\Form1.cs    28    28    TestRequest 
    Error    5    Cannot use local variable 'strm' before it is declared    C:\**\TestRequest\Form1.cs    29    22    TestRequest 
    Error    6    The name 'sbSoap' does not exist in the current context    C:\**\TestRequest\Form1.cs    41    33    TestRequest 
    Error    7    The name 'sbSoap' does not exist in the current context    C:\**\TestRequest\Form1.cs    47    49    TestRequest 

    Im sorry, if its to simple, but im trying to understand it untill now and its still working i only need help or suggestions.

    i did some changes:
                StringBuilder sbSoap = new StringBuilder(); 
     
                XmlTextReader doc = new XmlTextReader("RequestXML.xml"); 
                //XmlDocument doc = new XmlDocument(); 
                doc.Read(); 
                //doc.LoadXml(sb.Tostring()); 
                doc.Save(sbSoap); 
                //1. Build SOAP string  


    • Edited by MartienoS Tuesday, February 10, 2009 11:45 AM
    Tuesday, February 10, 2009 11:28 AM
  • Hi,
    I was only telling you how I got a response (though with some server error) from server.
    You can use the code you mentioned in the beginning with some minor changes.
    Instead of the following in your code
    Stream strm = req.GetRequestStream(); 
                    byte[] bytes = 
                    System.Text.Encoding.ASCII.GetBytes(sbSoap.ToString()); 
                    strm.Write(bytes, 0, bytes.Length); 
                    strm.Close(); 
    You can use the following.
    Stream strm = req.GetRequestStream(); 
    XMlDocument doc = new XMLDocument()
    doc.LoadXML(sb.Tostring())
    doc.Save(strm).


    This has worked for me. Essentially we are doing the same in different way, you are using bytes array and I am using xml document to serialized the soap message into the request stream.

    Be Happy
    Tuesday, February 10, 2009 11:44 AM
  • Oke well.. maybe im stupid but.. the XmlDocument doesnt support Save..

    This is my code now:

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Net; 
    using System.IO; 
    using System.Xml; 
     
     
    namespace TestRequest 
        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
            } 
     
            private void Request_Click(object sender, EventArgs e) 
            { 
                XmlDocument sbSoap = new XmlDocument(); 
                sbSoap.Load("RequestXML.xml"); 
     
                string soapAction = "http://www.bascomputers.nl/GetProductSpecifications"
     
                //2. Send XML to CDR server  
                string cdrURL = "http://xtract.bascomputers.nl:4040/xtract.asmx"
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL); 
     
                req.ContentType = "text/xml; charset=utf-8"
                req.Accept = "text/xml"
                req.Method = "POST"
                req.ContentLength = sbSoap.Length; 
                req.Headers.Add("SOAPAction", soapAction); 
                req.KeepAlive = false
     
                Stream strm = req.GetRequestStream();  
                //XmlDocument doc = new XmlDocument(); 
                //doc.LoadXML(sb.Tostring()); 
                //doc.Save(strm); 
     
                //3. Get response & send back filename  
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
     
                StreamReader r = new 
                StreamReader(resp.GetResponseStream()); 
     
                XmlTextReader xmlRead = new XmlTextReader(r); 
     
                while (xmlRead.Read()) 
                { 
                    //Console.Write(xmlRead.Value.ToString()); 
                    String newValue = xmlRead.Value.ToString(); 
                    this.status.Text = newValue
                } 
     
                r.Close(); 
                strm.Close();  
     
            } 
     
            private void Form1_Load(object sender, EventArgs e) 
            { 
     
            } 
        } 
     

    Error:
    Error   1   'System.Xml.XmlDocument' does not contain a definition for 'Long' and no extension method 'Long' accepting a first argument of type 'System.Xml.XmlDocument' could be found (are you missing a using directive or an assembly reference?)   C:\Documents and Settings\Martyn\Bureaublad\OUD\Test2\TestRequest\TestRequest\Form1.cs  37  40  TestRequest 
     

    Tuesday, February 10, 2009 12:01 PM
  •  
    Use the following code.  

    try  
                {  
                    string soapRequest=  
                        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + "\n"  +  
                        "<soap:Body>  " + "\n" +  
                        "<m:GetProductSpecifications xmlns:m='http://www.bascomputers.nl/'>"  + "\n" +  
                        "<m:customerID>123456</m:customerID>"+ "\n" +  
                        "<m:password>wachtwoord</m:password>" + "\n" +  
                        "</m:GetProductSpecifications>"   + "\n" +  
                        "</soap:Body>"   + "\n" +  
                        "</soap:Envelope>" + "\n" ;  
     
                        
                    string soapAction = "http://www.bascomputers.nl/GetProductSpecifications";  
     
                    //2. Send XML to CDR server     
                    string cdrURL = "http://xtract.bascomputers.nl:4040/xtract.asmx";  
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL);  
     
                    req.ContentType = "text/xml; charset=utf-8";  
                    req.Accept = "text/xml";  
                    req.Method = "POST";  
                    //req.ContentLength = sbSoap.Length;  
                    req.Headers.Add("SOAPAction", soapAction);  
                    req.KeepAlive = false;  
                   XmlDocument doc = new XmlDocument();    
                    doc.LoadXml(soapRequest);    
                    using (Stream strm = req.GetRequestStream())  
                    {  
                        doc.Save(strm);  
                    }  
                    //XmlDocument doc = new XmlDocument();    
                    //  doc.LoadXML(soapRequest);    
                        
     
                    //3. Get response & send back filename     
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();  
     
                    //StreamReader r = new 
                    //StreamReader(resp.GetResponseStream());  
                    using (Stream respStm = resp.GetResponseStream())  
                    {  
                        using (StreamReader r = new StreamReader(respStm))  
                        {  
                              
                                string str = r.ReadToEnd();  
                             
                        }  
                    }  
                    
                } 

    Be Happy
    • Marked as answer by MartienoS Tuesday, February 10, 2009 12:51 PM
    Tuesday, February 10, 2009 12:35 PM
  • Vinnuu, your the best im getting a full response! all the data is sended to me :)
    You made the code :D Thank You!

    Ill will code untill its finnished when i have problems (with the new code) i will post a new topic :)
    Tuesday, February 10, 2009 12:53 PM