EodData wsdl java connection - java

I am stuck trying to figure out how to initiate a WSDL connection with EodData.com
the wsdl address is
http://ws.eoddata.com/data.asmx?wsdl
I am using CXF to create a client connection:
QName qname = new QName("http://ws.eoddata.com/Data", "Data");
Data data = new Data(new URL("http://ws.eoddata.com/data.asmx?wsdl"), qname);
DataHttpGet dataGet = data.getDataHttpGet();
dataGet.login("xxx", "ppp");
and I got
Caused by: org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Header"). Expected elements are <{http://ws.eoddata.com/Data}LoginResult>
Not sure how I should initiate the connection?

A quick test using the following code worked for me.
Using WSDL2Java:
wsdl2java -autoNameResolution http://ws.eoddata.com/data.asmx?wsdl
Then using the code you provided with a few changes:
QName qname = new QName("http://ws.eoddata.com/Data", "Data");
Data data = new Data(new URL("http://ws.eoddata.com/data.asmx?wsdl"), qname);
DataSoap dataSoap = data.getDataSoap();
LOGINRESPONSE response = dataSoap.login("xxx", "ppp");
System.out.println(response.getMessage());
The response was:
Invalid Username or Password

Related

Getting ​Transfer-Encoding=chunked instead of content length in soap ws call

I am trying to call two soap ws and from java. When I'm calling these ws from two different java thread it's successfully called but when tried to call in same thread, first call get successful and second call get stuck. I can see both the request in my logs.
I checked tcp dump at server and can see for first request, all the header parameter is set correctly but in second call instead of content-length getting transfer-encoding = chunked.
first ws call header - 2/15/2018 9:59:40 AM [8]
Content-Length=639 Content-Type=text/xml; charset=UTF-8 Accept=/ Host=test102.com User-Agent=Apache CXF
2.7.11 SOAPAction="Trackem.Web.Services/ReserveServiceTime" Proxy-Connection=Keep-Alive
Second ws call header - 2/15/2018 10:01:11 AM [9]
Transfer-Encoding=chunked Content-Type=text/xml; charset​​=UTF-8 Accept=/ Host=test102.com
User-Agent=Apache CXF 2.7.11
SOAPAction="Trackem.Web.Services/CreateOrUpdateTask"
Proxy-Connection=Keep-Alive5:05 PM
Please help me do understand why second call is not working properly?
Here is my java ws method -
public P getPort(final Class<P> serviceEndpointInterface, final String ascNode) throws MalformedURLException{
final Bus currThreadBus = BusFactory.getThreadDefaultBus();
ClassLoader originalThreadClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader busFactoryClassLoader = BusFactory.class.getClassLoader();
try {
Thread.currentThread().setContextClassLoader(busFactoryClassLoader);
BusFactory.setThreadDefaultBus(BusFactory.newInstance().createBus());
QName qname = new QName(nameSpace, strQName);
Service service = Service.create(qname);
P port = null;
if (CommonUtil.isEmpty(portName)) {
port = service.getPort(serviceEndpointInterface);
} else {
QName portQname = new QName(nameSpace, portName);
port = service.getPort(portQname, serviceEndpointInterface);
}
BindingProvider bp = (BindingProvider) port;
// Timeout in millis
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
bp.getRequestContext().put(Message.CONNECTION_TIMEOUT, Integer.parseInt(connectTimeout));
bp.getRequestContext().put(Message.RECEIVE_TIMEOUT, Integer.parseInt(requestTimeout));
final Client client = ClientProxy.getClient(port);
client.getOutInterceptors().add(new LoggingOutInterceptor());
client.getInInterceptors().add(new LoggingInInterceptor());
//Add proxy server details if configured in ASC
if(!CommonMethods.isEmpty(proxyHost) && !CommonMethods.isEmpty(proxyPort))
{
HTTPConduit http = (HTTPConduit) ClientProxy.getClient(port).getConduit();
http.getClient().setProxyServer(proxyHost);
http.getClient().setProxyServerPort(Integer.parseInt(proxyPort));
if(!CommonMethods.isEmpty(proxyUsername) && !CommonMethods.isEmpty(proxyPassword))
{
http.getProxyAuthorization().setUserName(proxyUsername);
http.getProxyAuthorization().setPassword(proxyPassword);
}
}
return port;
}finally {
BusFactory.setThreadDefaultBus(currThreadBus);
Thread.currentThread().setContextClassLoader(originalThreadClassLoader);
}}
I solved this issue by disabling chunk transfer in HTTP client.
http.getClient().setAllowChunking(false);
I guess, the problem was in my proxy serve see this link

How to set JSON payload on AmazoneWebServiceRequest

I am using AmazonHttpClient & AmazonWebServiceRequest to make http requests to API gateway from android app. The requestBody is a JSON String and DefaultRequest only seem to accept InputStream as content.
final AmazonWebServiceRequest awsRequest = new AmazonWebServiceRequest() {};
final Request request = new DefaultRequest(awsRequest, UtilConstants.API_GATEWAY_SERVICE_NAME);
request.setEndpoint(uri);
request.setHttpMethod(requestType);
request.addHeader(HttpHeader.CONTENT_TYPE, "application/json");
final InputStream stream = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
request.setContent(stream);
When I make the request I get following exception
Caused by: com.amazonaws.AmazonClientException: Unable to execute HTTP request: expected 0 bytes but received 38
adding HttpHeader.CONTENT_LENGTH header solved the issue.

Dropwizard Jersey client Multipart http request

Dropwizard (Version 0.8.2) uses Jersey internally to provide HTTP client. I am using this client to send a Multipart POST request to an external Rest Endpoint to a SMS Service. Code is given below but it doesn't seems to be working because i am not receiving any message through this method also it does not throw any error.
URI for the first sample is http://enterprise.com/GatewayAPI/rest?userid=%s&password=%s&method=xlsUpload&filetype=zip&msg_type=TEXT&auth_scheme=PLAIN&v=1.1
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(fileName, file,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("fileName", fileName).bodyPart(fileDataBodyPart);
Entity<FormDataMultiPart> entity =
Entity.entity(multiPart, multiPart.getMediaType());// MediaType.MULTIPART_FORM_DATA_TYPE)
Client tenacityClient = TenacityJerseyClientBuilder
.builder(AppDependencyKeys.BULK_SMS)
.usingTimeoutPadding(Duration.milliseconds(500)).build(client)
.register(MultiPartFeature.class);
Invocation invocation = getResourceBuilder(tenacityClient, uri).buildPost(entity);
Future<Response> futureResponse = invocation.submit();
long start = System.currentTimeMillis();
futureResponse.get();
But the same works with below method when i use Apache Commons Httpclient. working code for the same is given below.
HttpClient client = new HttpClient();
PostMethod method = new
PostMethod("http://enterprise.com/GatewayAPI/rest");
Part[] parts = {
new StringPart("method", "xlsUpload"),
new StringPart("userid", "*******"),
new StringPart("password", "*******"),
new StringPart("filetype", "zip"),
new StringPart("v", "1.1"),
new StringPart("auth_scheme", "PLAIN"),
new FilePart(file.getName(), file)
};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
int statusCode = client.executeMethod(method);
log.info("Status code: {}", statusCode);
But i want to use the first way as that suits my infrastructure better.
I think you should set up properly media type for entity. Currently, you created new FormDataMultiPart but, you did not set and media type and it uses "text/plain" y default.
So, you should set up MediaType.APPLICATION_OCTET_STREAM_TYPE to your FormDataMultiPart as media type.

PHP RESTful API accessing Jersey Client POST data

I am using a PHP RESTful API which is consumed by a Java desktop application using jersey 2.21.
Usually, when I send a POST request from AngularJS, I can access the data via:
$data = json_decode(file_get_contents("php://input"));
However, when I use jersey, the data is put in the $_POST array. Here is my Jersey code:
final HashMap<String, String> params = // some hashmap with a few entries
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
MultivaluedMap formData = new MultivaluedHashMap(params);
WebTarget target = client.target(url);
// Get JSON
String jsonResponse = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);
return jsonResponse;
How do I post the data from Jersey so that I can access it via the above PHP call?
I know I can just use the $_POST array, but I need this API to be consumed from a mobile app, Java desktop & an AngularJS app.
Thanks in advance
Thanks to #jonstirling, I got it.
I had to set the content type as JSON. here is the updated code:
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(url);
String data = new Gson().toJson(params);
// POST JSON
Entity json = Entity.entity(data, MediaType.APPLICATION_JSON_TYPE);
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
String jsonResponse = builder.post(json, String.class);
return jsonResponse;

Send XMLRPC Request using Java

I am trying to send an XMLRPC Request via Java and is unsuccessful. Here's the structure of XMLRPC Request that I need to send with method name create.account:
<createaccount>
<functioncode>bank_account</functioncode> <cardnumber>55553263654898</cardnumber>
<transaction_id>12345678</transaction_id>
<transactiondatetime>2012-01-08 14:12:22</transactiondatetime>
</createaccount>
As per client, I should be expecting the following XMLRPC Response:
<createaccount>
<code>200</code>
<message>SUCCESS</message>
<functioncode>bank_account</functioncode>
<cardnumber>55553263654898</cardnumber>
<transaction_id>12345678</transaction_id>
<transactiondatetime>2012-01-08 14:12:22</transactiondatetime>
</createaccount>
I have made the following snippet in java but I'm getting an error: 'Failed to create input stream: Server returned HTTP response code: 500 for URL'
Here's the snippet:
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(server_url));
XmlRpcClient client = new XmlRpcClient();
config.setBasicUserName(pUser);
config.setBasicPassword(pPassword);
client.setConfig(config);
Map m = new HashMap();
m.put("functioncode", "bank_account");
m.put("cardnumber", "55553263654898");
m.put("transaction_id", "12345678");
m.put("transactiondatetime", "2012-01-08 14:12:22");
Object[] params = new Object[]{m};
String result = (String)client.execute("bank.account", params);
System.out.println("Results:" + result);
How I can do this?
I would recommend using XML-RPC library, for example Redston XML-RPC. More info and tutorial can be found here.

Categories

Resources