I am at the end of my ropes so its time to ask the community for help, i've been pillaging the web for any resource I can find on this issue but none helps me.
I am currently trying to communicated with a Java Webservice server through PHP. I can pull down a WSDL and list its functions and types through __getFunctions(); and __getTypes();
I am creating the client in this format
new SoapClient("https://username:password#ip:port/path/to/wsdl?wsdl");
Now initially this poses no problems, but when I try to make a function call on this service I get 1 of 2 responses.
I know one of them is a timeout error, the second one I do not understand as of yet.
Error : SoapFault exception: [SOAP-ENV:Client] [MT-IP] SOAP message is not well formed in...
Here is the code:
// All of this works
$options["login"] = "login";
$options["password"] = "password";
$wsdl = "https://" . $options["login"] . ":" . $options["password"] . "#ip:port/path/to/wsdl?wsdl";
$client = new SoapClient($wsdl, $options);
try {
$functions = $client->__getFunctions();
$types = $client->__getTypes();
$params = new stdClass();
$params->pong = (string)"Hello World!";
// This fails
$result = $client->ping($params);
var_dump($result);
} catch (SoapFault $exception) {
echo $exception;
}
To add I have also attempted to call methods in all the ways available such as
$client->__soapCall("ping", array($params));
$client->__soapCall("ping", array(array("pong" => "Hello World!)));
$client->__soapCall("ping", array("pong" => "Hello World"));
$client->__soapCall("ping", array("parameters" => array("pong" => "Hello World"));
// and some more..
You can also see the WSDL I am testing against
WSDL
You can also view the page where I try to call webservice
SOAP TEST
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="com.computas.mt.extern.Ping">
<SOAP-ENV:Body>
<ns1:ping>
<pong>Hello World!</pong>
</ns1:ping>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In my case the schematics the server wanted was different than that of the request PHP was sending out. The solution was to physically change the request XML before it was sent out by extending the SoapClient. We found the changes to the issue once we had the XML layout that the server usually gets provided with.
class MySoapClient extends SoapClient {
function __doRequest( $request, $location, $action, $version, $one_way = NULL ) {
$request = str_replace("SOAP-ENV", "soapenv", $request);
$request = str_replace("xsi", "com", $request);
$request = str_replace("ns1", "com", $request);
var_dump($request);
return parent::__doRequest( $request, $location, $action, $version, $one_way );
}
}
Do not put username and password into the url. SoapClient accepts an option array that offers parameters for this purpose. http://de2.php.net/manual/en/soapclient.soapclient.php
Related
If You are sending an object to a webmethod to use it as prameter:
Example: sending an object that will be represented as a class in the server like this:
$creditCard = new stdClass();
$creditCard->number="705";
$creditCard->expiryDate="03/10/2019";
$creditCard->controlNumber=9;
$param = array("creditCard" => $creditCard);
$Response = $client->__soapCall('valider', array($param));
You won't get any property of the class CreditCard that is in the server to work, and every time you do creditClass.getNumber() or trying to use accessors you will get a null pointer error because your object is not really created.
To fix this problem you have to modify the server webservice and not the client: you have to add the trivial jax-ws annotation #webParam:
public String valider(#WebParam(name = "creditCard")CreditCard creditCard){ ... }
And your PHP soapClient code is very simple: (if you want more security you have to encode your credentials or use an other way for authentication like digest authentication or other jax ws security implementations)
try {
$opts = array (
'http' => [
'header' => "username: myrealname\r\n".
'password: pass1myrealpassword']
);
$par= array(
'trace' => 1,
'exceptions' =>0,
'soap_version' => SOAP_1_1,
'connection_timeout' => 1800,
'stream_context' => stream_context_create($opts),
);
$client = new SoapClient($wsdl,$par);
//...... and call your service method as you do
I'm implementing a client to a web service (and the guys maintaining the web service have been a litte unresponsive..) I've used axis and WSDL2Java to generate java classes and I can call their login-method on their authentication-service ok, and get a sessionId back (eg z4zojhiqkw40lj55kgtn1oya). However, it seems that i cannot use this sessionId as a parameter anywhere. Even a call to their hasSession()-method directly after login returned false. I managed to solve this by setting setMaintainSession(true) on the Locator-object for this service. But the problem is, that this first service, the Authentication-service, is only used for authentification. If I then call setMaintainSession(true) on eg ProductServiceLocator, and call some method on it, I will get an error because of unauthenticated session. I have to find a way to share the session between the services on the client side.
Looking on their php code example-it seeems like they are storing the session in a cookie. How can I mimic this behaviour in my java client?
php-code:
$authentication = new SoapClient ( "https://webservices.24sevenoffice.com/authenticate/authenticate.asmx?wsdl", $options );
// log into 24SevenOffice if we don't have any active session. No point doing this more than once.
$login = true;
if (!empty($_SESSION['ASP.NET_SessionId'])){
$authentication->__setCookie("ASP.NET_SessionId", $_SESSION['ASP.NET_SessionId']);
try{
$login = !($authentication->HasSession()->HasSessionResult);
}
catch ( SoapFault $fault ) {
$login = true;
}
}
if( $login ){
$result = ($temp = $authentication->Login($params));
// set the session id for next time we call this page
$_SESSION['ASP.NET_SessionId'] = $result->LoginResult;
// each seperate webservice need the cookie set
$authentication->__setCookie("ASP.NET_SessionId", $_SESSION['ASP.NET_SessionId']);
// throw an error if the login is unsuccessful
if($authentication->HasSession()->HasSessionResult == false)
throw new SoapFault("0", "Invalid credential information.");
}
My code is the following:
AuthenticateLocator al = new AuthenticateLocator();
al.setMaintainSession(true);
Credential c = new Credential(CredentialType.Community,username,password,guid);
AuthenticateSoap s = al.getAuthenticateSoap();
String sessionId = s.login(c);
System.out.println("Session id was: "+sessionId);
System.out.println("Has Session: "+s.hasSession()); //Hooray, now works after setMaintainSession(true)
//And now trying to call another Service
CompanyServiceLocator cl = new CompanyServiceLocator();
cl.setMaintainSession(true);
CompanyServiceSoap css = cl.getCompanyServiceSoap();
css.getCountryList(); //FAILS!
So what can I do to make this work?
Hooray, I finally solved it myself :-D
Thanx a lot to the excellent article at http://www.nsftools.com/stubby/ApacheAxisClientTips.htm
I had to do the following with my code to make it work:
CompanyServiceLocator cl = new CompanyServiceLocator();
cl.setMaintainSession(true);
CompanyServiceSoap css = cl.getCompanyServiceSoap();
((Stub)css)._setProperty(HTTPConstants.HEADER_COOKIE, "ASP.NET_SessionId="+sessionId); //New line that does the magic
css.getCountryList(); //SUCCESS :-D
Operating in the high-level abstraction of the autogenerated classes, it was unknown to me that casting the service classes to Stub would expose more methods and properties that could be set. Good to know for later I guess :-)
I want to send the data to person object. How to do it with PostMethod.
def payload ='<person><nationalId>'+1234567+'</nationalId></person>'
def method = new PostMethod(url)
def client = new HttpClient()
payload = payload.trim()
method.addRequestHeader("Content-Type","text/xml")
method.addRequestHeader("Accept","text/xml,application/xml;q=0.9")
Credentials credentials = new UsernamePasswordCredentials('simple', 'simple');
client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST,8080, AuthScope.ANY_REALM, "digest"),credentials);
method.setRequestEntity(new StringRequestEntity(payload))
def statusCode = client.executeMethod(method)
println "STATUS CODE : ${statusCode}"
def resultsString = method.getResponseBodyAsString()
method.releaseConnection()
println resultsString
I tried above coding. How to set password and username and password digest also. For that i think status code 400 is coming.Please notify where i made mistake
Try to look at REST Client Builder Plugin. See docs and I guess, you'll find more convenient way to send request
So I'm trying to send an xml-rpc message to moses xml-rpc server in Java, but keep getting error:
org.apache.xmlrpc.XmlRpcException: Parameter that is supposed to be a structure is not
I'm using Apache xmlrpc client 3.1.3 from http://ws.apache.org/xmlrpc/client.html.
My guess is the parameter definition is not compatible, but after experimenting with different type to use as input, the output is still the same. I have an example of the client, but it's written in perl:
#!/usr/bin/env perl
use Encode;
use XMLRPC::Lite;
use utf8;
$url = "http://localhost:8080/RPC2";
$proxy = XMLRPC::Lite->proxy($url);
$text = "il a souhaité que la présidence trace à nice le chemin pour l' avenir .";
# Work-around for XMLRPC::Lite bug
$encoded = SOAP::Data->type(string => Encode::encode("utf8",$text));
my %param = ("text" => $encoded, "align" => "true");
$result = $proxy->call("translate",\%param)->result;
print $result->{'text'} . "\n";
if ($result->{'align'}) {
print "Phrase alignments: \n";
$aligns = $result->{'align'};
foreach my $align (#$aligns) {
print $align->{'tgt-start'} . "," . $align->{'src-start'} . ","
. $align->{'src-end'} . "\n";
}
}
and here is my code:
XmlRpcClientConfigImpl tConf = new XmlRpcClientConfigImpl();
try {
tConf.setServerURL(new URL("http://127.0.0.1:8080/RPC2"));
tConf.setBasicEncoding("UTF-8");
} catch (MalformedURLException ex) {
ex.printStackTrace(System.out);
}
XmlRpcClient tClient = new XmlRpcClient();
tClient.setConfig(tConf);
List<List<String>> tInPar = new ArrayList<>();
tInPar.add(Arrays.asList(new String[]{"text", "hello"}));
tInPar.add(Arrays.asList(new String[]{"align", "true"}));
String tResult = null;
try {
tResult = (String) tClient.execute("translate", tInPar);
} catch (XmlRpcException ex) {
ex.printStackTrace(System.out);
}
Is it correct?
Thank you for your help
After consulting in moses mailing list, I've been given this java client example for moses server: https://github.com/moses-smt/mosesdecoder/blob/master/contrib/server/SampleClient.java
Thanks
I don't know Java and can't check your code. If your client has any other connection method besides XML transmitted via SOAP, use it! This has been true for any SOAP and most XML APIs I've seen in the past.
Did you run that Perl code and verify that it's working?
If it does: Add some debugging to see what it does and compare this to your script. XMLRPC::Lite claims to be based on SOAP::Lite which has debugging when using
use SOAP::Lite +trace;
Three chances: First, adding this line enables debugging also for XMLRPC::Lite. Second: Add the "+trace" flag to the "use XMLRPM::Lite" line to enable debugging. Third: Maybe SOAP debugging is impossible when being called as XMLRPC::Lite.
Look here for more information: http://metacpan.org/pod/SOAP::Trace
Finally, a packet sniffer like tcpdump may show you what is being transmitted.
I'd suspect that either your XML structure differs from the Perl one's or they differ only by whitespaces, linebreaks or order of elements and the server at the other side is not accepting true XML but a text block with keywords included in < >.
I am using BlazeDS java client to get info from this page.
This page has a form in the middle that when you select a type, the location combo on the button gets updated.
I am trying to use BlazeDS to get those values in java.
I have been using Charles web proxy to debug, and this are the screenshots from the request and the response:
My code so far is the following:
// Create the AMF connection.
AMFConnection amfConnection = new AMFConnection();
// Connect to the remote url.
String url = "http://orlandoinfo.com/flex2gateway/";
try
{
amfConnection.connect(url);
}
catch (ClientStatusException cse)
{
System.out.println(cse);
return;
}
// Make a remoting call and retrieve the result.
try
{
// amfConnection.registerAlias("flex.messaging.io.ArrayCollection", "flex.messaging.io.ArrayCollection");
amfConnection.call("ColdFusion.getLocations", new Object[] {"consumer", "attractions", "ATTR"});
}
catch (ClientStatusException cse)
{
System.out.println(cse);
}
catch (ServerStatusException sse)
{
System.out.println(sse);
}
// Close the connection.
amfConnection.close();
When I run it I get a:
ServerStatusException
data: ASObject(15401342){message=Unable to find source to invoke, rootCause=null, details=null, code=Server.Processing}
HttpResponseInfo: HttpResponseInfo
code: 200
message: OK
Can anyone spot what's wrong?
Thanks for reading!
I ended up using Charles Web Proxy. Sniffing AMF parameters and running my code with -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888
I compare both calls and modify to look alike.
The working code looks like this:
String url = "http://www.theGateWayurl.com";
// Generates the connection to the amf gateway.
AMFConnection amfConnection = new AMFConnection();
// Must register the class that this library will use to load the
// AMF object information.
// The library will read AMF object variables and use setters from
// the java bean stated in this line.
AMFConnection.registerAlias("", new LabelData().getClass().getName());
try {
// Do the connection.
amfConnection.connect(url);
// This page requires a certain headers to function.
// The Content-type is used to sniff with Charles Web Proxy.
amfConnection.addHttpRequestHeader("Content-type", "application/x-amf");
// The Referer is used by the webpage to allow gathering information.
amfConnection.addHttpRequestHeader("Referer", "http://orlandoinfo.com/ws/b2c/sitesearch/customtags/comSearch.swf");
// The rest of the HTTP POST sent by this library is wrapped
// inside a RemotingMessage.
// Prepare the msg to send.
RemotingMessage msg = new RemotingMessage();
// The method called in the server.
msg.setOperation("getLocations");
// Where the request came from. Similar to referer.
msg.setSource("ws.b2c.sitesearch.components.myService");
// The destination is a needed parameter.
msg.setDestination("ColdFusion");
// Create the body with the parameters needed to call the
// operation set with setOperation()
msg.setBody(new Object[] {"consumer", "attractions"});
// This is needed but not used.
msg.setMessageId("xxxxxxxxxx");
// Send the msg.
AcknowledgeMessage reply = (AcknowledgeMessage) amfConnection.call("null", msg);
// Parse the reply from the server.
ArrayCollection body = (ArrayCollection) reply.getBody();
for (Object obj : body) {
LabelData location = (LabelData) obj;
// Do something with the info.
}
} catch (ClientStatusException cse) {
// Do something with the exception.
} catch (ServerStatusException sse) {
// Do something with the exception.
} finally {
amfConnection.close();
}
The LabelData is just a java bean with with two vars: Data and Label.
I tried to comment every line for a better understanding.
Take into account what Stu mention in previous comments about crossdomain.xml to see if you have the rights to do this kind of things.