Android HttpUrlConnection和特定站点日志

Android HttpUrlConnection和特定站点日志,android,Android,有人能帮我写一个程序,连接到网站,让我登录,并获取登录网站的内容吗?我希望它为网站工作 有这么多的重定向和POST请求,我不知道这是否可能。请帮帮我 我试过类似的方法,但不起作用 dateFormat = new SimpleDateFormat(DATE_FORMAT); store = new ArrayList<Map<String, String>>(); String uRL = "http://www.imperiaonline.or

有人能帮我写一个程序,连接到网站,让我登录,并获取登录网站的内容吗?我希望它为网站工作 有这么多的重定向和POST请求,我不知道这是否可能。请帮帮我

我试过类似的方法,但不起作用

    dateFormat = new SimpleDateFormat(DATE_FORMAT);
    store = new ArrayList<Map<String, String>>();

    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 && sCookie.length() > 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<String, String> cookie = new HashMap<String, String>();
                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 < 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();
    }
dateFormat=新的SimpleDataFormat(日期格式);
store=newarraylist();
字符串uRL=”http://www.imperiaonline.org";
StringBuilder响应=新建StringBuilder();
Log.v(“执行”,“执行url:+url”);
试一试{
URL=新URL(URL);
HttpURLConnection httpconn=(HttpURLConnection)url
.openConnection();
if(sCookie!=null&&sCookie.length()>0){
httpconn.setRequestProperty(“Cookie”,sCookie);
}
if(httpconn.getResponseCode()==HttpURLConnection.HTTP\u确定){
BufferedReader输入=新BufferedReader(
新的InputStreamReader(httpconn.getInputStream()),8192);
字符串strLine=null;
而((strLine=input.readLine())!=null){
追加应答(strLine);
}
input.close();
}
字符串headerName=null;
for(int i=1;(headerName=httpconn.getHeaderFieldKey(i))!=null;i++){
if(headerName.equalsIgnoreCase(SET_COOKIE)){
Map cookie=newhashmap();
StringTokenizer st=新的StringTokenizer(
httpconn.getHeaderField(i),COOKIE\u值\u分隔符);
//规范规定第一个名称/值对
//字符串中是cookie名称和值,所以让我们
//处理
//作为特例:
如果(st.hasMoreTokens()){
字符串标记=st.nextToken();
字符串名称=令牌。子字符串(0,
token.indexOf(名称值分隔符);
字符串值=token.substring(
token.indexOf(名称、值、分隔符)+1,
token.length());
添加(cookie);
cookie.put(名称、值);
}
而(st.hasMoreTokens()){
字符串标记=st.nextToken();
曲奇(
token.substring(0,
token.indexOf(名称\值\分隔符))
.toLowerCase(),
令牌子串(
token.indexOf(名称、值、分隔符)+1,
token.length());
}
}
}
}捕获(例外e){
e、 printStackTrace();
}
对于(int i=0;i
看一看示例

private HttpClient mHttpClient;
private HttpContext mContext;
private CookieStore mCookieStore;

public void post(){
        ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("uname", "your username"));
        list.add(new BasicNameValuePair("password", "your username"));
        list.add(new BasicNameValuePair("form_submit", "Login"));
        task mTask = new task();
        mTask.execute(list);
    }
    public class task extends AsyncTask<ArrayList<BasicNameValuePair>, Void, String>{

        @Override
        protected String doInBackground(ArrayList<BasicNameValuePair>... arg0) {
            if(mHttpClient == null){
               mHttpClient = new DefaultHttpClient();
             }
             if(mContext == null){
                mContext = new BasicHttpContext();
             }
             if(mCookieStore == null){
                 mCookieStore = new BasicCookieStore();
             }
            mContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore)
            String sResponse = "";
            HttpResponse response = null;
            try{

                HttpPost httppost = new HttpPost("http://www99.imperiaonline.org/imperia/game_v5/game/ajax_login.php");
                httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
                mHttpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
                response = mHttpClient.execute(httppost, mContext);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                StringBuilder s = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }

                // Getting cookies 
                ArrayList<Cookie> list = new ArrayList<Cookie>(mCookieStore.getCookies());
                for(Cookie cookie : list){
                   Log.i(TAG, "cookie: " + cookie.getName());
                }
            }catch(IOException e){

            }
            return sResponse;
        }

        @Override 
        protected void onPostExecute(String result){
            System.out.println(result);
        }

    }
私有HttpClient mHttpClient;
私有HttpContext-mContext;
私人库克商店;
公共空缺职位(){
ArrayList=新建ArrayList();
添加(新的BasicNameValuePair(“uname”、“您的用户名”);
添加(新的BasicNameValuePair(“密码”、“您的用户名”);
添加(新的BasicNameValuePair(“表单提交”、“登录”);
任务mTask=新任务();
mTask.execute(列表);
}
公共类任务扩展了异步任务{
@凌驾
受保护的字符串doInBackground(ArrayList…arg0){
if(mHttpClient==null){
mHttpClient=新的DefaultHttpClient();
}
if(mContext==null){
mContext=新的BasicHttpContext();
}
if(mCookieStore==null){
mCookieStore=新的BasicCookieStore();
}
setAttribute(ClientContext.COOKIE\u存储,mCookieStore)
字符串sResponse=“”;
HttpResponse响应=null;
试一试{
HttpPost HttpPost=新的HttpPost(“http://www99.imperiaonline.org/imperia/game_v5/game/ajax_login.php");
setEntity(新的UrlEncodedFormEntity(arg0[0]);
mHttpClient.getParams().setParameter(ClientPNames.COOKIE_策略,CookiePolicy.BROWSER_兼容性);
response=mHttpClient.execute(httppost、mContext);
BufferedReader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);