Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:12:40+00:00 2026-05-28T04:12:40+00:00

I am trying to send the POST data from java to a PHP page.

  • 0

I am trying to send the POST data from java to a PHP page. However it is not working. Whatever I echo in the php page works fine but when I send data it gives- ‘undefined index’
What could be the problem ?
This is my java file.

import java.net.*;
import java.io.*;

class Main {
public static void main(String args[]) throws IOException {

    URL url = new URL("http://localhost/CD/user/test");
    String result = "";
    String data = "fName=" + URLEncoder.encode("Atli", "UTF-8");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    try {

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        // Send the POST data
        DataOutputStream dataOut = new DataOutputStream(
                connection.getOutputStream());
        dataOut.writeBytes(data);
        dataOut.flush();
        dataOut.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String g;
        while ((g = in.readLine()) != null) {
            result += g;
        }
        in.close();

    } finally {
        connection.disconnect();
        System.out.println(result);
    }

}
}

And here is my php controller:

public function test(){

    $test=$_POST['fName'];
    $all="This is a ";
    $all=$all." ".$test;
    echo $all;



}  

When I just send a url request , I get the ouput as ‘This is a’. So it is connecting to the url and everything but while sending data, it is not working. Please help! Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:12:40+00:00Added an answer on May 28, 2026 at 4:12 am

    You are using different streams for posting and getting.
    Your post code works fine.

    replace:

     BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream())); // different stream
    

    with

    DataInputStream in = new DataInputStream (connection.getInputStream ()); // same connection
    

    And it should work fine.

    // edit: here without any deprecated methods:

        BufferedReader in = null;
        try {
            String line;
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: iPhone: Post data to php page Passing Data From iPhone app to
I'm trying to send a lot of data from a form using the $.post
I am trying to send data from dynamicly generated cheboxes(when ticked) to my php
I am trying to send data from an Android app to a PHP file
I'm tying to send POST data from one site to another (both sites have
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to send data from a client application using jQuery to a REST
I am trying to send an email message with data collected from an html
I am trying to get some data from the user and send it to
I am trying to send data(a username and password) to an online form from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.