I have a code that sends a request with post parameters. However, when I check on server, the post array is empty. I've tried everything I found in internet, but nothing helped.What is an wrong here?
String url = "http://exifeed.com/api.php"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); con.setRequestMethod("POST"); String urlParameters = "ApiKey=xxx"; // Send post request con.setDoOutput(true); OutputStream os = con.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(urlParameters); writer.flush(); writer.close(); os.close(); con.connect(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(StringEscapeUtils.unescapeJava(response.toString())); Code on server
print_r($_GET); print_r($_POST); exit; The output result is
Array()Array()
P.S. I added www. before domain name and it suddenly worked. I guess it was because I had redirect in .htaccess file on server. Thanks everyone anyway.
file_get_contents('php://input');to access the content of the post body.