栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android:如何以编程方式登录网站并从中检索数据?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android:如何以编程方式登录网站并从中检索数据?

为此,您必须发送两个POST请求。在第一个请求中,成功登录后,您需要发送登录数据并保存cookie。在第二个请求中,您需要发送已保存的cookie,您可以获取数据。POST的数据必须采用以下格式:var
= value&var2 = value2

在您的情况下:

String data = "studKnNr=login&asmKodas=password";

以及请求网址:https :
//medeine.vgtu.lt/studentams/submit.jsp

请参见以下代码:

String data = "studKnNr=login&asmKodas=password";String loginUrl = "https://medeine.vgtu.lt/studentams/submit.jsp";String Login = POST_req(loginUrl, data, 10000); //And after succcessful login you can send the second request:String pageContent = POST_req(someUrl, "", 10000);//Methods for sending requests and saving cookie: //(this doesn't need any changes, you can just paste it in your project)public String POST_req(String url, String post_data, int len) {    URL addr = null;    try { addr = new URL(url);        } catch (MalformedURLException e) { return "Incorrect URL";        }        StringBuffer data = new StringBuffer();        HttpURLConnection conn = null;        try { conn = (HttpURLConnection) addr.openConnection();        } catch (IOException e) { return "Open connection error";        }        conn.setRequestProperty("Connection", "keep-alive");        conn.setRequestProperty("Accept-Language", "ru,en-GB;q=0.8,en;q=0.6");        conn.setRequestProperty("Accept-Charset", "utf-8");        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*        int status;        try { status = conn.getResponseCode();        } catch (IOException e2) { return "Response error";        }        if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { String new_url = conn.getHeaderField("Location"); String cookies = conn.getHeaderField("Set-cookie"); URL red_url; try {     red_url = new URL(new_url); } catch (MalformedURLException e) {     return "Redirect error"; } try {     conn = (HttpURLConnection) red_url.openConnection(); } catch (IOException e) {     return "Redirect connection error"; } //conn.setRequestProperty("Content-type", "text/html"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("Accept-Language", "ru,en-GB;q=0.8,en;q=0.6"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); conn.setRequestProperty("cookie", cookies);      conn.setDoOutput(true); conn.setDoInput(true); //conn.setInstanceFollowRedirects(true);        }        java.io.InputStream in = null;        try { in = (java.io.InputStream) conn.getInputStream();        } catch (IOException e) { return "In stream error";        }        InputStreamReader reader = null;        try { reader = new InputStreamReader(in, "UTF-8");        } catch (UnsupportedEncodingException e) { return "In stream error";        }        char[] buf = new char[len];        try { reader.read(buf);        } catch (IOException e) { return "In stream error";        }        get_cookie(conn);        return (new String(buf));    }    public void get_cookie(HttpURLConnection conn) {        SharedPreferences sh_pref_cookie = getSharedPreferences("cookies", Context.MODE_PRIVATE);        String cook_new;        String cookieS_HEADER;        if (conn.getHeaderField("Set-cookie") != null) { cookieS_HEADER = "Set-cookie";        }        else { cookieS_HEADER = "cookie";        }        cook_new = conn.getHeaderField(cookieS_HEADER);        if (cook_new.indexOf("sid", 0) >= 0) { SharedPreferences.Editor editor = sh_pref_cookie.edit(); editor.putString("cookie", cook_new); editor.commit();        }    }    public void set_cookie(HttpURLConnection conn) {        SharedPreferences sh_pref_cookie = getSharedPreferences("cookies", Context.MODE_PRIVATE);        String cookieS_HEADER = "cookie";        String cook = sh_pref_cookie.getString(cookieS_HEADER, "no_cookie");        if (!cook.equals("no_cookie")) { conn.setRequestProperty(cookieS_HEADER, cook);        }    }

当然,您必须在异步线程中发送请求。

希望对您有所帮助。请原谅我的英语不好:)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号