Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST request with URLConnection, only numbers seems to work
    primarykey
    data
    text
    <p>I'm trying to do a request to a server with some POST parameters, I have used some code that I found here: <a href="https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests">Using java.net.URLConnection to fire and handle HTTP requests</a></p> <p>The problem is that all values becomes "0" when i write them out on the php page on the server, except the first numbers in the ssn. Also, the response i get back to the java code, does not have "charset=UTF-8" in the "content-type" member of the header. But as you can see in the php/html code, I don't change the header anywhere. </p> <p>Android code: </p> <pre><code>public static String testCon() { String url = "http://xxx.xxx.se/postReciverTest.php"; String charset = "UTF-8"; String param1 = "Test"; String param2 = "Test2"; String param3 = "123456-7899"; // ... String query = null; try { query = String.format("fname=%s&amp;sname=%s&amp;ssn=%s", URLEncoder.encode(param1, charset), URLEncoder.encode(param2, charset), URLEncoder.encode(param3, charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } URLConnection connection = null; try { connection = new URL(url).openConnection(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } connection.setDoOutput(true); // Triggers POST. connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); OutputStream output = null; try { try { output = connection.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } try { output.write(query.getBytes(charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } finally { if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} } InputStream response = null; try { response = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); } int status; try { status = ((HttpURLConnection) connection).getResponseCode(); } catch (IOException e) { e.printStackTrace(); } for (Entry&lt;String, List&lt;String&gt;&gt; header : connection.getHeaderFields().entrySet()) { System.out.println(header.getKey() + "=" + header.getValue()); } String contentType = connection.getHeaderField("Content-Type"); charset = null; for (String param : contentType.replace(" ", "").split(";")) { if (param.startsWith("charset=")) { charset = param.split("=", 2)[1]; break; } } charset = "UTF-8"; //this is here just because the header don't seems to contain the info and i know that the charset is UTF-8 String res = ""; if (charset != null) { BufferedReader reader = null; try { try { reader = new BufferedReader(new InputStreamReader(response, charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { for (String line; (line = reader.readLine()) != null;) { // ... System.out.println(line) ? res += line; } } catch (IOException e) { e.printStackTrace(); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} } } else { // It's likely binary content, use InputStream/OutputStream. } return null; } </code></pre> <p>page that i sent the request to:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt; Test &lt;? echo $_POST["fname"] + "&lt;br /&gt;"; echo $_POST["sname"] + "&lt;br /&gt;"; echo $_POST["ssn"] + "&lt;br /&gt;"; ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>So the result I get in the "res" variable is the html code with "00123456" insted of: "Test Test2 123456-7899"</p> <p>This is not my field, so it would be nice if the answer(s) is fairly easy to understand :)</p> <p>Thanks in advance! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload