menu


Subscribe to the blog

Social networks

My page on vk.com
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Post java - Send POST request in Java

Programming Date of creation: Views: 378

Post java - Send POST request in Java

lesnih.info


Russia

Send POST request to java

Below, the post-request code for the java application is placed
package postjava;
 
import java.io.DataOutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
 
 
 
 
public class PostJava {
   
    public static void main(String[] args) throws Exception {
    
    
        String url = "https://vitaly.lesnih.info/post.php";              
        String param = "text=hello world!";
       
        URL url_post_java = new URL(url);
        HttpsURLConnection Post_Java = (HttpsURLConnection) url_post_java.openConnection();
 
 
        // Send post request
        Post_Java.setDoOutput(true);
        DataOutputStream write_post = new DataOutputStream(Post_Java.getOutputStream());
        write_post.writeBytes(param);
 
 
        Post_Java.getResponseCode();
 
        System.out.println("Ready!");        
    }   
}

POST request receiver in PHP

We create a file post.php, which in the future we place on hosting

<?

$text = $_POST["text"];

$fd = fopen("post.txt", 'w') or die("не удалось создать файл");
$str = $text."\n;"
fwrite($fd, $str);
fclose($fd);

echo $str;
?>

<form method="post">
    
    <input  name="text"  value="">
    <input type="submit" name="submit"></input>
    
</form>

And also, it is necessary in advance to create a file post.txt, which will be in the same folder on the server as post.php

This post.php file will accept the send request from the Java application, and in order for us to see that the request was sent the data will be written to the post.txt file

After the query is executed in the Java application, you need to open the text document post.txt


Post java - Send POST request in Java
Rating: 5 | Тotal votes: 1 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Generator Binary code online

    Generator Binary code online
    Topic: Programming

    Tags: binary code, online resource,generator

    Views:1185

    How to send email to JSP Java

    How to send email to JSP Java
    Topic: Programming

    Tags: java,jsp,send email,email

    Views:680

    Installing Apache Tomcat to work in Java JSP

    Installing Apache Tomcat to work in Java JSP
    Topic: Programming

    Tags: java,jsp,сервер,apache tomcat

    Views:266