Home > Software engineering >  Problem with communication with React-Native and ASP.NET WebService
Problem with communication with React-Native and ASP.NET WebService

Time:12-03

Edited

I have a problem with communication between ASP.NET web service and react-native. I try to use ajax request after reading this: Simplest SOAP example and finally get error message. Error code bellow:

"DONE": 4, "HEADERS_RECEIVED": 2, "LOADING": 3, "OPENED": 1, "UNSENT": 0, 
"_aborted": false, "_cachedResponse": undefined, "_hasError": true, 
"_headers": {"content-type": "text/xml"}, 
"_incrementalEvents": false, "_lowerCaseResponseHeaders": {}, 
"_method": "POST", "_perfKey": 
"network_XMLHttpRequest_http://localhost:44358/BikeWebService.asmx", 
"_performanceLogger": {"_closed": false, "_extras": {}, 
"_pointExtras": {}, "_points": {"initializeCore_end": 11271328.84606, 
"initializeCore_start": 11271264.66206}, "_timespans": 
{"network_XMLHttpRequest_http://localhost:44358/BikeWebService.asmx": 
[Object]}}, "_requestId": null, "_response": "Failed to connect to localhost/127.0.0.1:44358", 
"_responseType": "", "_sent": true, "_subscriptions": [], "_timedOut": false, 
"_trackingName": "unknown", "_url": "http://localhost:44358/BikeWebService.asmx", 
"readyState": 4, "responseHeaders": undefined, "status": 0, "timeout": 0, "upload": {}, withCredentials": true

Ajax request in react native:

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', 'http://localhost:44358/BikeWebService.asmx', true);

    var sr ='<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">\
    <s:Header>
     <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/LogIn</Action>
    </s:Header>
    <s:Body>\
      <LogIn xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">\
        <password>a</password>\
        <login>1</login>\
      </LogIn>\
    </s:Body>\
    </s:Envelope>';

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);
            }
        }
        console.log(xmlhttp);
    }

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(sr);

In ASP.NET, I have a web method like this:

[WebMethod]
public ResponseModel<User> LogIn(string password, string login)
{
    return new User();
}

I tried for this answer using axios, but still getting network error. Make request to SOAP endpoint using axios

I also try add cors to web.config as shown in this answer: How to allow CORS for ASP.NET WebForms endpoint? But it's still not working...

SSL is disabled in ASP.NET service. I try to disabled firewall and tls in windows but this is not a problem I'm running on ASP.NET 4.8

Does anybody have any idea? Is XMl okey? I have it from WCF test client.

CodePudding user response:

Starting web service on IIS and add address to hosts on windos fix this connection error.

SOAP request:

let body = '<?xml version="1.0" encoding="utf-8"?>\
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">\
  <soap12:Body>\
    <LogIn xmlns="http://tempuri.org/">\
      <password>string</password>\
      <login>string</login>\
    </LogIn>\
  </soap12:Body>\
</soap12:Envelope>';

let SOAPAction = 'http://tempuri.org/LogIn';

requestOptions = {
 method: 'POST',
 body: body,
 headers: {
   Accept: '*/*',
   'SOAPAction': SOAPAction,
   'Content-Type': 'text/xml; charset=utf-8'
 },
};


await fetch('http://ip:port/BikeWebService.asmx', requestOptions)
.then((response) =>  console.log(response))
.catch((error) => {
    console.log('er'   error)
});
  • Related