Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to resolve 404 returned from web method when not running on localhost
#1
I'm calling a webmethod that has been around for a while. When hitting localhost, all is fine and I get a 200 response. Here is soap envelope:

Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetAccountInfo"

<?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>
<GetAccountInfo xmlns="http://tempuri.org/">
<signinID>string</signinID>
<password>string</password>
</GetAccountInfo>
</soap:Body>
</soap:Envelope>

Now when the python code attempts to hit the hosted url, I am getting a 404 response. Here is the python side code I am using:

    def api_login(self, username, password):
        parameters = {'signinID': username, 'password': password}
        args = urllib.urlencode(parameters)

        request = urllib2.Request(self.ApiLoginCheck+"/GetAccountInfo", args)
        request.add_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
        request.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
        response = urllib2.urlopen(request)

        self.last_response_code = response.code
        self.last_response_content = response.read().decode('utf-8')
        if response.code == 200:
            print(response)
            print(self.last_response_content)
            root = ET.fromstring(self.last_response_content)
            xmlns = {'t': 'http://tempuri.org/'}
            emails = root.findall('.//t:accountEmail', namespaces=xmlns)
            for email in emails:
                print(email)

            if email.text == '' or email.text == None:
                return 0
            else:
                return 1
        else:
            return 0
Quote
#2
print out value of request just after line 7, try running from browser.
Quote
#3
Request format is unrecognized for URL unexpectedly ending in /GetAccountInfo

for a soap web service should I be able to post with urllib in the manner I'm trying, or would I have to build the XML soap envelope and post the request that way?
Quote
#4
Definitely not as pretty, or as elegant as I was hoping. I was able to get things working by constructing the whole soap envelope (incl header) and posting that.

   def api_login3(self, username, password):
        SM_TEMPLATE = """<?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>
            <GetAccountInfo xmlns="http://tempuri.org/">
              <signinID>%s</signinID>
              <password>%s</password>
            </GetAccountInfo>
          </soap:Body>
        </soap:Envelope>
                """

        SoapMessage = SM_TEMPLATE % (username, password)

        print(SoapMessage)

        request = urllib2.Request(self.ApiLoginCheck, SoapMessage)
        request.add_header("Host", self.ApiHost)
        request.add_header("User-Agent", "Python post")
        request.add_header("Content-type", "text/xml; charset=\"UTF-8\"")
        request.add_header("Content-length", "%d" % len(SoapMessage))
        request.add_header("SOAPAction", "http://tempuri.org/GetAccountInfo")
        print(request)
        response = urllib2.urlopen(request)

        self.last_response_code = response.code
        self.last_response_content = response.read().decode('utf-8')
        if response.code == 200:
            print(response)
            print(self.last_response_content)
            root = ET.fromstring(self.last_response_content)
            xmlns = {'t': 'http://tempuri.org/'}
            emails = root.findall('.//t:accountEmail', namespaces=xmlns)
            for email in emails:
                print(email)

            if email.text == '' or email.text == None:
                return 0
            else:                
                return 1
        else:
            return 0

For completeness, here are the 2 urls that helped me reach my solution:

Python XML HTTP Post to send a SOAP message

urllib2 — extensible library for opening URLs
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  What is the use of call method and when to use it? everyday1 1 57 Jul-14-2019, 01:02 PM
Last Post: ichabod801
  Method not defined diskt 1 111 Jul-03-2019, 12:55 PM
Last Post: noisefloor
  Method not regocnized Lass86 3 182 Jun-19-2019, 09:03 AM
Last Post: Lass86
  web3 method rembrandt 3 148 Jun-04-2019, 05:43 PM
Last Post: buran
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 156 May-22-2019, 07:20 AM
Last Post: buran
  I always get 'None' returned. Confused. What did I miss? jpezz 2 161 Apr-07-2019, 10:06 AM
Last Post: jpezz
  How to retrieve and save attachments returned by SOAP web service ashwsrin 0 179 Feb-20-2019, 07:12 AM
Last Post: ashwsrin
  TypeError: __str__ returned non-string error jolinchewjb 5 724 Jan-24-2019, 07:54 AM
Last Post: jolinchewjb
  problems when running a signrawtransaction method wectri 1 296 Dec-04-2018, 04:53 PM
Last Post: Larz60+
  function wanted: resolve relative path Skaperen 4 454 Sep-06-2018, 01:52 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)