Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HttpUrlConnection and specific site logging
    primarykey
    data
    text
    <p>Can anyone help me in writing a program that will connect to site, log me in, and get content of the logged site? I want it to work for site <a href="http://www.imperiaonline.org" rel="nofollow">http://www.imperiaonline.org</a> There are so many redirects and POST requests that I don't know if it is possible. Please help me.</p> <p>I've tried something like this, but it doesn't work...</p> <pre><code> dateFormat = new SimpleDateFormat(DATE_FORMAT); store = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); String uRL = "http://www.imperiaonline.org"; StringBuilder response = new StringBuilder(); Log.v("Execute", "execute url:" + uRL); try { URL url = new URL(uRL); HttpURLConnection httpconn = (HttpURLConnection) url .openConnection(); if (sCookie != null &amp;&amp; sCookie.length() &gt; 0) { httpconn.setRequestProperty("Cookie", sCookie); } if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader input = new BufferedReader( new InputStreamReader(httpconn.getInputStream()), 8192); String strLine = null; while ((strLine = input.readLine()) != null) { response.append(strLine); } input.close(); } String headerName = null; for (int i = 1; (headerName = httpconn.getHeaderFieldKey(i)) != null; i++) { if (headerName.equalsIgnoreCase(SET_COOKIE)) { Map&lt;String, String&gt; cookie = new HashMap&lt;String, String&gt;(); StringTokenizer st = new StringTokenizer( httpconn.getHeaderField(i), COOKIE_VALUE_DELIMITER); // the specification dictates that the first name/value pair // in the string is the cookie name and value, so let's // handle // them as a special case: if (st.hasMoreTokens()) { String token = st.nextToken(); String name = token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR)); String value = token.substring( token.indexOf(NAME_VALUE_SEPARATOR) + 1, token.length()); store.add(cookie); cookie.put(name, value); } while (st.hasMoreTokens()) { String token = st.nextToken(); cookie.put( token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR)) .toLowerCase(), token.substring( token.indexOf(NAME_VALUE_SEPARATOR) + 1, token.length())); } } } } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i &lt; store.size(); ++i) { for (String element : store.get(i).keySet()) { Log.i("ARR", element); } } Log.i("HTML", response.toString()); try { StringBuilder postDataBuilder = new StringBuilder().append("uname=").append(URLEncoder.encode("zygmunter", "UTF8")); postDataBuilder.append("password=").append(URLEncoder.encode("zygmunter", "UTF8")); byte[] postData = null; postData = postDataBuilder.toString().getBytes(); URL url = new URL("http://www99.imperiaonline.org/imperia/game_v5/game/ajax_login.php"); HttpURLConnection co = (HttpURLConnection) url.openConnection(); co.setDoOutput(true); co.setRequestMethod("POST"); co.setRequestProperty("Content-Length", Integer.toString(postData.length)); co.setUseCaches(false); OutputStream out = co.getOutputStream(); out.write(postData); out.close(); response = new StringBuilder(); if (co.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader input = new BufferedReader( new InputStreamReader(co.getInputStream()), 8192); String strLine = null; while ((strLine = input.readLine()) != null) { response.append(strLine); } input.close(); } String resp = response.toString(); Log.i("WYNIK", resp); } catch (IOException ioe) { ioe.printStackTrace(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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