Web service for java desktop

Asked

Viewed 41 times

0

I have a question. How is the request made for a php page using java desktop? Should I create another project for java web?

  • You want to request from within a desktop system to a web or the other way around?

  • Exactly. I have a desktop system, which is a sales program, and I need to send the information to a php page through the POST. But I’m not getting this operation.

1 answer

0

You can use java.net.

Example:

try {
        String urlParameters  = "param1=a&param2=b&param3=c";
        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int    postDataLength = postData.length;
        String request        = "http://example.com/index.php";
        URL    url            = new URL( request );
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
        conn.setDoOutput( true );
        conn.setInstanceFollowRedirects( false );
        conn.setRequestMethod( "POST" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
        conn.setRequestProperty( "charset", "utf-8");
        conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
        conn.setUseCaches( false );
        try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
            wr.write( postData );
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  • Also I could not. Only a question in urlParameters I have to put the String that the POST will request? I did it this way: String input = "{"operation":"registration","type":"representative","name":"admin"}";

  • I updated the example

  • Even so, it doesn’t work.

  • But what happens, some error appears ? It even runs the request?

  • I’ll show you the code I made for the request. On the php page, it checks if there really is something in the isset method. If you don’t have any information, you enter Else and it appears that no data has been sent. But the strange thing is that put System.out.println appear the data.

  • Client c = Client.create(); Webresource Wb = c.Resource("http://localhost:9090/Nasu/test.php"); String input; String output; input = "{"operation":"register","type":"representative","name":"admin"}"; System.out.println(input); Clientsponse Response = Wb.type("application/json"). post(Clientresponse.class, input); output = Response.getEntity(String.class); System.out.println(output); Response.close();

  • See in the example the parameter part " String urlParameters = "param1=a&param 2=b&param 3=c";"

  • I filled in the fields like this: operation=registration & type=representant&name=admin

  • But no message appears

  • In php you are capturing the parameters with $_POST?

  • Yes. It is that way: if(isset($_POST['operation'))

  • If you enter the condition captures the data, otherwise the message of data not sent appears.

  • But it’s enough to submit, just not send the data?

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.