3
Hello, I have a problem that I haven’t been able to solve for days.
The situation is as follows, until today I only managed to use the GET method of my webservice by passing parameters directly to the path of the URL. But now I need to consume the webservice by passing a parameter that is a String
JSON for the webserice to validate this JSON and return me the data I need in another JSON via POST method.
Webservice POST method code:
@POST
@Produces("application/json")
@Path("/VerificaID")
@Consumes(MediaType.APPLICATION_JSON)
public String getDados(@QueryParam("id_senha_uev") String id_senha_uev){
Uev uev = new Uev();
Gson gson = new Gson();
java.lang.reflect.Type uevType = new TypeToken<Uev>() {}.getType();
uev = gson.fromJson(id_senha_uev, uevType);
Gson g = new Gson();
if(uev.getID_Uev() == 123 && uev.getSenha() == 123){
return g.toJson(DadosRequisitados);
}
else{
return g.toJson(null);
}
And here the client method:
// HTTP GET request
private String sendGet(String Url, String Json) throws Exception {
try{
URL targetUrl = new URL(null, Url, new sun.net.www.protocol.https.Handler());
HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Accept", "application/json");
String input = Json;
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();
if (httpConnection.getResponseCode() != 443) {
throw new RuntimeException("Failed : HTTP error code : " + httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));
String output;
System.out.println("Output from Server:\n");
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
}
httpConnection.disconnect();
responseBuffer.close();
return responseBuffer.toString();
}catch(MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
Someone would know how to send a parameter for the authentication in the webservice and it returns the data to me, without using the absolute path of the URL?
Thanks in advance.
How does this authentication work or should work? Just send a JSON and get the answer or are you trying to use something more complex such as basic-Authorization, Digest or Oauth?
– Victor Stafusa
The Uev object has id and password as attributes, the json has id and password for authentication, all I want to do is authenticate this json and if validated, return a string to the client that would be the requested data that he requested.
– Yuri Pires