Calling a web service with no wsdl - java

I want to write a Java program to call a web service. WSDL is not available for this web service. I have written programs to call a web service which has wsdl. Here I don't have any idea of how I can proceed. Not able to find many samples in Internet as well.
Is there any better frame work which I can use? I am getting JSON output from web service.
I am looking at options of writing a best possible case(If I could write a generalized program which could be used for many web services with out much changes, it would be great)

Well there are several ways to consume rest service.
Using Spring framework:
import org.springframework.web.client.RestTemplate
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject("http://localhost:8080/users/2", User.class);
System.out.println("Username: " + user.getUsername());
Using apache httpclient:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://localhost:8080/users/2");
HttpResponse response = httpClient.execute(getRequest);
HttpEntity httpEntity = response.getEntity();
String userString = EntityUtils.toString(httpEntity);
// Transform 'userString' into object using for example GSON:
Gson gson = new Gson();
User user = gson.fromJson(userString, User.class);
System.out.println("Username: " + user.getUsername());

Related

Firebase Dynamic Link Rest API - 400 Bad Request

I am transitioning an existing service from using google url shortener api to try and use Firebase Dynamic Links. I have linked a project from the Google Cloud Platform, and setup a "dummy" android app so that I can have the app domain for the dynamic links. I am trying to use the REST API to shorten urls for very long urls that can't be handled by a third party. I have tried sending using:
ObjectMapper mapper = new ObjectMapper();
HttpPost httpPost = new HttpPost("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=****");
FirebaseDynamicLinkInfo dynamicLinkRequest = new FirebaseDynamicLinkInfo();
dynamicLinkRequest.setDynamicLinkDomain("zw5yb.app.goo.gl");
dynamicLinkRequest.setLink(assetUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);
I am getting a 400 Bad Request when I post the request to the API (on the httpCLient.execute line. I have double checked my api-key. I have also tried using just the longDynamicLink parameter, and it gets the 400 Bad Request Response.
Any ideas of where I could be going wrong?
Thanks,
Ben
I contacted Google Support on this one, and I wasn't UrlEncoding my querystring parameters on the deep link. After encoding the link, the request was successful. I went back to using passing json that just had a longDynamicLink property (as opposed to the dynamicLinkInfo object in my original post). Here is what it looks like:
String myEscapedUrl = "https://zw5yb.app.goo.gl/?link=" + URLEncoder.encode(assetUrl, "UTF-8");
FirebaseDynamicLinkRequest dynamicLinkRequest = new FirebaseDynamicLinkRequest(myEscapedUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
// inform the server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);

Post to SharePoint 2013 from Java

I've tried to connect to our SharePoint and POST some data to a list.
A user can interact with a Web-App and send some Information. These data will be send to a Java-Web-Interface running on a tomcat. The Java-Code should connect to our SharePoint and post the data in the list. Today, I read a lot of tutorials and ressources on the web... Most of them are deprecated ore discuss lightly different situations! SO! My mind whispered: "Go on and visit stackoverflow." And here I am, asking this question:
The Situation is described above. I call a web-Interface vie JS (angularJS) and pass an E-Mail-Adress which the user enters in the front-end. Here it goes in:
#Path("webservice")
public class SetEmail {
#POST
#Path("/SetEmail")
#Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8")
#Produces("text/plain")
public String addItem(String incoming) throws ClientProtocolException, IOException, AuthenticationException{
String result = "error";
JSONObject jsonObj = new JSONObject(incoming);
String listName = "Leads";
String username = "...";
char[] password= new char[]{'...', '...', ...};
String website = "...";
Now, after all I read, I have to get the DigestValue from SharePoint, because I want to make a POST-Request:
//Get the Digestvalue.
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new NTCredentials(username, password.toString(), "http://...", "https://..."));
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpPost httpPost = new HttpPost(website + "_api/contextinfo");
httpPost.addHeader("Accept", "application/json;odata=verbose");
httpPost.addHeader("content-type", "application/json;odata=verbose");
httpPost.addHeader("X-ClientService-ClientTag", "SDK-JAVA");
HttpResponse response = client.execute(httpPost);
byte[] content = EntityUtils.toByteArray(response.getEntity());
String jsonString = new String(content, "UTF-8");
System.out.println(response);
JSONObject json = new JSONObject(jsonString);
String FormDigestValue = json.getJSONObject("d").getJSONObject("GetContextWebInformation").getString("FormDigestValue");
After getting the Digest, I am able to execute the actual request:
//POST the data.
CloseableHttpClient client2 = HttpClients.createDefault();
HttpPost httpPost2 = new HttpPost(website + "_api/web/lists/GetByTitle(" + listName + ")");
httpPost2.setEntity(new StringEntity("test post"));
NTCredentials creds = new NTCredentials(username, password.toString(), "http://...", "https://...");
httpPost2.addHeader(new BasicScheme().authenticate(creds, httpPost2, null));
httpPost2.addHeader("X-RequestDigest", FormDigestValue);
httpPost2.addHeader("Accept", "application/json;odata=verbose");
httpPost2.addHeader("Content-Type", "application/json;odata=verbose");
CloseableHttpResponse response2 = client2.execute(httpPost2);
System.out.println(response2);
client2.close();
}
}
I know this isn't the most beautiful Code and yes, I am not an Java expert. My Problems are:
I don't know weather all of these code-Fragments are up to date or
weather I am using deprecated ones. Perhaps someone is able to
enlighten me.
I am using HttpClient from Apache. To me it looked like the most
usable library. Is that right?
Everytime I execute the Action on the front-end and my Code starts
running, I am getting an HTTP 401 Unauthorized error. I tried
various Kinds of Code but none worked well.
HttpResponseProxy{HTTP/1.1 401 Unauthorized [Server: Microsoft-IIS/8.0, SPR..
Perhaps someone has the Patience to tell me how to do it. Thank you.
Whoa... you are really trying some black magic here ;) - I would suggest you to get your HTTP POST / GET in a tool like Postman or some other REST tool working and then return to your code.
I don't know exactly what you are trying to achieve, but it might be easier to go via powershell (if you are trying to create a migration script) or JavaScript (if you are on a website).
Be aware that authentication differs in SharePoint online and SharePoint on premise... this is also customizable by your company (you can for example implement forms-based auth as well). Be sure to know what YOUR SharePoint is using. (Or share some more info, so we can help)

Getting results of WFS request to GeoServer as UTF-8

I have the following code to send a WFS request to a locally running GeoServer instance using the Apache http-client-4.1 library:
ThreadSafeClientConnManager connectionMngr = new ThreadSafeClientConnManager();
DefaultHttpClient httpClient = new DefaultHttpClient(this.connectionMngr);
HttpPost httpPost = new HttpPost(wfsUrl);
httpPost.setEntity(new StringEntity(wsft, HTTP.UTF_8));
log.debug("Submitting WSFT request: " + wsft);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String result = httpClient.execute(httpPost, responseHandler);
log.debug("Result of WSFT request: " + result);
The data I am retrieving from the GIS database is encoded in UTF-8, and all the features I would expect to find are found. However, any special characters are not being printed properly by my debug statements, or being displayed properly in the front end of my application (a Spring MVC web app).
I know the values are being stored correctly in my GIS database as I can see them via SQL client and they are printed as I would expect. I can also see the names of Roads etc which use special characters are being printed properly on my map layers which suggests the GeoServer is configured correctly.
Instead of passing in a BasicResponseHandler, use the HttpClient.execute(HttpUriRequest) method which returns a HttpResponse and then use EntityUtils.toString(HttpEntity, "UTF-8"), pseudo:
HttpResponse r = httpClient.execute(httPost)
String utf8encodedEntity = EntityUtils.toString(r.getEntity(), "UTF-8");

Using asp webservice with android

I'm trying to access this link for web service via android .
but I don't know what is library or jar files should using to do this .
I have used soap but not working will
here is link is :
http://ictfox.com/demo/Hafil_Updates/Login_Check.aspx?UserLogin=Demo&Password=Demo
I think you can use DefaultHttpClient android api to send GET request as you shown.
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI webserv = new URI(" http://ictfox.com/demo/Hafil_Updates/Login_Check.aspx?UserLogin=Demo&Password=Demo");
request.setURI(webserv);
HttpResponse response = httpclient.execute(request);
String responseStr = EntityUtils.toString(response.getEntity());
this is tutorial show you how to call android application webservice using protocol soap
http://android.programmerguru.com/how-to-call-asp-net-web-service-in-android/

send JSON android object to .net server

I have .net web service that expect JSON object. This web service has method for return token:
public string GetToken(string username, string password)...
This is my site url for direct access (when I field manually id, method and params and paste in browser url I receive response )
http://mysite.com/JsonRPC.aspx?id={0}&method={1}&params={2}
On stackoverflow I found way to create and send JSON object in android , here is example:
HttpPost request = new HttpPost(URL);
JSONStringer json = new JSONStringer()
.object()
.key("username").value("username")
.key("password").value("password")
.endObject();
Log.i("json",json.toString());
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
My problem is that I don't know how to call my .net web service. What format should be URL variable , where to specific method name , and how to specific method parameters ?
Please give me code
Thanks
I would use this library.
http://code.google.com/p/android-json-rpc/
There are examples on the website that should be sufficient for you.

Categories

Resources