简体   繁体   中英

Getting HTML instead of JSON data while using HTTP 'GET' as request-method

I am making a login GUI for my android app, where I'm trying to get JSON data through to my android app by using HTTP request 'GET' (for username + password). The problem is that I am receiving HTML data and it's driving me crazy. I have a theory that perhaps the login-function sets a number of cookies (for example, JSESSIONID), and that they are not being saved (therefore one cannot log in) however i'm not entierly sure. Have I missed specifying something in the code?

Also, when I print the cookies variable in the logcat, I only get a bunch of null values.

The code:

 private class DownloadStartTilesData extends AsyncTask<String, Void, String> {

    String jsonResponse = "";
    String urlRequest;

    private List<String> cookies;
    static final String COOKIES_HEADER = "Set-Cookie";

    @Override
    protected String doInBackground(String... params) {

        //    Log.d("JSON", "Json: " + urlRequest);

        URL url;
        HttpURLConnection client;

        try {

            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);

            url = new URL(urlRequest);

            client = (HttpURLConnection) url.openConnection();
            client.setRequestMethod("GET");
            client.setRequestProperty("Connection", "Keep-Alive");

            // Cookie stuff:
            if (cookies != null) {
                for (String cookie : this.cookies) {
                    client.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
                }
            }

         (iPhone)");
            client.setRequestProperty("Content-Type", "Logon/json");
            client.setDoOutput(true);
            client.setDoInput(true);


            Log.d("Response:", "" + client.getResponseCode());
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()), 8192);
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                jsonResponse = jsonResponse.concat(inputLine);
                System.out.println(inputLine);
            }


            in.close();

            setCookies(client.getHeaderFields().get("Set-Cookie"));


            // out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {
        BackendURLs backendURLs = new BackendURLs(BuildConfig.SERVER_URL);


    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    // More cookies stuff:
    public void setCookies(List<String> cookies) {
        this.cookies = cookies;
    }
}

}

The jsonResponse actually gives HTML. Any help or pinpointing to solve my problem would be greatly appreciated.

HTML response:

<!DOCTYPE html>
<html lang="sv" xml:lang="sv">
     <head>
      <title>Generic Application Error Test JSP (Item)</title>
         <link rel="stylesheet" href="/wcsstore/SharedStorefrontAssetStore/include/styles/style1/css/desktop.css" type="text/css" media="screen"/>
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
     </head>
    <body>

         <div class="page">
            <header>
                 <div class="line  size1of1">
                    <div class="unit size1of3">
                        <ul class="mod fz_12px border-bottom_3px header-list-top bold">
                            <li class="not-active-store">
                               <a 
                            <li class="not-active-store">
                        </ul>
                   </div>
                    <div class="unit size2of3">
                        <ul class="mod fz_12px border-bottom_3px header-list-top bold border-left">
                            <li class="not-active-store">
                               &nbsp;Application Error
                           </li>
                        </ul>
                     </div>
               </div>
              <div class="line major-header-line border-bottom_3px">
                   <div class="unit size2of3">
                       <div class="logo-search-field">
                             <img class="logo-header" src="/wcsstore/SharedStorefrontAssetStore/include/styles/style1/images/logo.png"/>
                         </div>
                  </div>
               </div>
           </header>
             <section class="mainWrap">
                 <article class="line">
                   <div class="margin-top_20px margin-bottom_20px">
                        <h2>Ett fel har intr&auml;ffat</h2>
                       <p>Det verkar som n&aring;got har g&aring;tt lite galet p&aring; webbsidan. Visa de tekniska detaljerna nedan, kontakta sedan kundtj&auml;nst om du inte kan l&ouml;sa felet sj&auml;lv.</p>
                         <p><a href="#" onclick="jQuery('#errorDetails').show();">Visa tekniska detaljer</a></p>
                       <div class="details" id="errorDetails" style="display: none">

                          <p><strong>TYP</strong>: 0</p>
                           <p><strong>KEY</strong>: _ERR_CMD_CMD_NOT_FOUND</p>
                           <p><strong>MSG</strong>: CMN3101E The system is unavailable due to &#034;_&#034;.</p>
                           <p><strong>SYS</strong>: Command not found: &#034;_&#034;.</p>
                             <p><strong>ORG</strong>: </p>

                         </div>
                  </div>
                </article>
            </section>

            <footer class="box bottom" style="padding-bottom: 4px;">
            </footer>
      </div>
     </body>
 </html>

Apparently, the problem lay with cookies as I had previously suspected. As a redirect occured, the cookies (of which the logon and password ID were located) were lost and therefore the JSON data request was processed without any logon credentials (hence the error HTML message).

By manually stopping the first redirect with the following code:

    connection.setInstanceFollowRedirects(false);

I was able to save the cookies before connecting once more:

    String cookies = connection.getHeaderFields().get("Set-Cookie").toString().replaceAll("\\[|\\]", "");

    URL url2 = new URL(connection.getHeaderField("Location"));
    HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection();
    connection2.setRequestProperty("Cookie", cookies);

I'm not sure why a redirect would eat up all the cookies but, there you have it guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM