登录Android网站时出现400 Bad Request

时间:2015-02-22 15:00:05

标签: java android

每当我尝试登录我的应用程序上的网站时,我都会收到400 BadRequest错误。但是当我使用普通的javaprogramm做同样的事情时它工作正常。

login-methode:

public void sendPost(String url, String postParams) throws Exception {

    URL obj = new URL(url);
    conn = (HttpsURLConnection) obj.openConnection();

    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Host", "www.XXX.de");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept",
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    conn.setRequestProperty("Connection", "keep-alive");
    conn.setRequestProperty("Referer", "https://XXX.de/YYY/");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();
    wr.close();
    Log.w("App", "sendPost: ResponseCode: " + conn.getResponseCode());
    responseCode = conn.getResponseCode();
    james.setResponseCode_SendPost(responseCode);

    BufferedReader in = 
             new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();  
  } // end of sendPost

在我开始使用sendPost()之前,我总是首先下载页面(没有BadRequest):

public String getPageContent(String url) throws Exception {
    URL obj = new URL(url);
    conn = (HttpsURLConnection) obj.openConnection();
    conn.setHostnameVerifier(hostnameVerifier);
    conn.setRequestMethod("GET");

    conn.setUseCaches(false);

    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept",
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    if (cookies != null) {
        for (String cookie : this.cookies) {
            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
        }
    }

    james.setResponseCode_GetPageContent(conn.getResponseCode());
    int x = conn.getResponseCode();
    Log.w("App", "ResponseCode: " + x);
    BufferedReader in = 
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine + "\r\n");
    }
    in.close();

    setCookies(conn.getHeaderFields().get("Set-Cookie"));
    return response.toString();

  } // end of getPageContent

1 个答案:

答案 0 :(得分:0)

我自己发现了错误

OutputStream方法writeBytes()必须是

wr.writeBytes("request=" + postParams);

而不是

wr.writeBytes(postParams);