从android做POST请求的问题

时间:2014-04-01 19:20:56

标签: android django http httpurlconnection django-csrf

我正在尝试从Android应用程序登录Django服务器,从网络上工作正常,但是当我尝试从应用程序执行此操作时,我收到内部服务器错误。我正在使用HttpURLConnection:

url = new URL(loginUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.getContent();
conn.disconnect();


CookieStore cookieJar =  cManager.getCookieStore();
List <HttpCookie> cookies = cookieJar.getCookies();
String csfr = null;
for (HttpCookie cookie: cookies) {
    Log.d("cookie", ""+cookie);
  if(cookie.getName()=="csrftoken"){
      csfr = cookie.getValue();
      break;
  }
}
String postParams = "csrfmiddlewaretoken="+csfr+"&username="+user+"&password="+pass+"&this_is_the_login_form=1&next=";

conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", ""+postParams.getBytes().length);
conn.setRequestProperty("User-Agent","Mozilla/5.0");
conn.setFixedLengthStreamingMode(postParams.getBytes().length);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(postParams);
dos.flush();
dos.close();
Log.d(conn.getResponseCode()+"", ""+conn.getResponseMessage());

GET请求正常工作并正确获取csrf cookie,我不知道我在POST请求中缺少什么。这是捕获浏览器请求发布的数据:

csrfmiddlewaretoken=zqvmoYTLeimB9RW5cMj5xTyLhIzR8kqr&username=user&password=123456&this_is_the_login_form=1&next=

编辑:

最后让它工作,diasabled csfr保护,添加了一些RequestProperty,读取响应(如果没有读取,在服务器端获得管道损坏错误)并在URL的末尾添加'/'。可能所有或几乎所有错误的问题都是网址上缺少的“/”。

最终工作编码:

url = new URL("http://XX.XXX.XXX.XXX/accounts/login/");
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.getContent();
conn.disconnect();

/*CookieStore cookieJar =  cManager.getCookieStore();
List <HttpCookie> cookies = cookieJar.getCookies();
String csfr = null;
for (HttpCookie cookie: cookies) {
  if(cookie.getName()=="csrftoken"){
      csfr = cookie.getValue();
      break;
  }
}*/
String postParams = "username=patient1&password=123456";

conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Length", ""+postParams.getBytes().length);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setFixedLengthStreamingMode(postParams.getBytes().length);
OutputStream os = conn.getOutputStream();
os.write(postParams.getBytes("UTF-8"));
InputStream is = conn.getInputStream();
while(is.read() > -1);

Log.d(conn.getResponseCode()+"", ""+conn.getResponseMessage());

1 个答案:

答案 0 :(得分:0)

您可能需要在django登录视图中禁用csrf protection