2014-04-01 65 views
1

我試圖從Android應用程序登錄Django服務器,從網絡上工作正常,但是當我嘗試從應用程序執行它時,出現內部服務器錯誤。我使用HttpURLConnection類:從Android執行POST請求的問題

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餅乾恰到好處,我不知道我缺少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

你是否在自己的後臺線程(如asyncTask)中執行此操作? – Eenvincible

+0

你可以請任何logcat錯誤,如果任何你得到。 – Rarw

+0

是的,它在一個asyncTask上,應用程序端沒有錯誤。 – Karibul

回答

0

您可能需要在你的Django登錄視圖禁用csrf protection

+0

我使用@csrf_exempt裝飾器製作了一個自定義登錄視圖,同樣適用於瀏覽器,但我仍然收到內部服務器錯誤:500 – Karibul

+0

您可以將DEBUG_MODE變量設置爲TRUE併發布您收到的錯誤嗎? – Bigcortex

+0

如何在Android應用程序中獲取此信息? – Karibul