0

我是一名新手,正在开发 Android 应用程序。我的应用程序运行良好,直到主机没有将他的网页设置为重定向到其他页面。现在我得到一个 html 页面代码

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Moved</TITLE>
</HEAD>
<BODY>
<H1>Object Moved</H1> this document may be found <a href="some url">here</a>
</BODY>
</HTML>

试过 conn.setInstanceFollowRedirects(false); 但仍然没有用。如果我使用 GET 方法,我会得到这个,但是在使用 POST 方法时,我会得到很多空格。请帮忙。

enter code here

这是我在 doinbackground() 中的代码

protected Void doInBackground(String... param) {
try
{
 HttpClient httpclient = new DefaultHttpClient(){
 @Override
  protected RedirectHandler createRedirectHandler() {
    return new DefaultRedirectHandler(){
  @Override
  public boolean isRedirectRequested(HttpResponse response,     HttpContext context) {

                                rep="httpclient redirect";
                                return false;
                            }
                        };
                    }
                };
                   HttpGet httppost = new HttpGet(param[0]);
                    httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,  false);
                   httppost.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
 ResponseHandler<String> responseHandler=new BasicResponseHandler();
                   String responseBody = httpclient.execute(httppost, responseHandler);
                   rep+=responseBody;


            }
 catch (Exception e)
            {
                if(e.getMessage()==null)
                    rep+="No response from server";
                else
                rep+="Error: " + e.getMessage();
            }
}`enter code here`
4

2 回答 2

0

所以...你的问题是什么?如果主机移动了文件,我们无法修复..您只需重定向到移动的资源并从那里获取信息..

于 2013-08-01T11:05:19.637 回答
0

如果我正确理解您的需求,我曾经遇到过这种情况。情况是您请求某个 url,响应是 HTTP 302 临时移动或类似的,但您需要来自该响应的信息。

尝试这个

 final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);


    // Create No Redirect HttpClient
    HttpClient httpClient = new DefaultHttpClient(){

        @Override
        protected RedirectHandler createRedirectHandler() {
            return new DefaultRedirectHandler(){
                @Override
                public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                    Log.d(TAG, "HttpClient Redirect");
                    return false;
                }
            };
        }
    };

    CookieStore cookieStore = new BasicCookieStore();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);


    // Prepare Login Request



    HttpPost postLogin = new HttpPost(URL_LOGIN);

    if(username == null || password == null){
        throw new LoginFailException();
    }


    String params1 = URLEncoder.encode("member[email]","utf-8")+"="+URLEncoder.encode(username,"utf-8");
    String params2 = "&"+URLEncoder.encode("member[crypted_password]","utf-8")+"="+URLEncoder.encode(password,"utf-8");
    String params3 = "&"+URLEncoder.encode("action","utf-8")+"="+URLEncoder.encode("login","utf-8");
    String params4 = "&"+URLEncoder.encode("redirect","utf-8")+"=";




    postLogin.setEntity(new StringEntity(params1+params2+params3+params4));
    postLogin.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postLogin.addHeader("Accept-Encoding", "gzip");
    postLogin.addHeader("User-Agent", "Dalvik/1.4.0 (Linux; U; Android 2.3.7; Full Android on x86 Emulator Build/GINGERBREAD)");



    HttpResponse respLogin = httpClient.execute(postLogin, httpContext);

从这个实现中,您可以从重定向响应中读取数据

于 2013-08-02T02:19:12.383 回答