我正在使用django服务器学习android网络。我可以发出GET请求和json响应,也可以获得csrf cookie,但是当我使用cookie并发出post请求时,django给出了403,说明csrf验证失败。在
我正在用csrf令牌设置所需的头“X-CSRFToken”,还传递了cookies(cookieString),但到目前为止没有成功。在
这是我在android上的课程。我可以使用python请求库发出post请求,所以django服务器没有问题,在这个android代码中有一些问题。请提出解决这个问题的任何方法。在private void signupUser() {
validateData();
new PostData().execute();
}
private void validateData() {
try {
urlParameters= URLEncoder.encode("code","utf-8")+"="+URLEncoder.encode("100","utf-8");
urlParameters+= "&"+URLEncoder.encode("username","utf-8")+"="+URLEncoder.encode(username,"utf-8");
urlParameters+= "&"+URLEncoder.encode("password","utf-8")+"="+URLEncoder.encode(password,"utf-8");
urlParameters+= "&"+URLEncoder.encode("firstName","utf-8")+"="+URLEncoder.encode(firstName,"utf-8");
urlParameters+= "&"+URLEncoder.encode("lastName","utf-8")+"="+URLEncoder.encode(lastName,"utf-8");
urlParameters+= "&"+URLEncoder.encode("email","utf-8")+"="+URLEncoder.encode(email,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private class PostData extends AsyncTask {
@Override
protected String doInBackground(Void... params) {
String response="";
try {
// Manage Cookies
String cookieString="";
String csrftoken="";
cookieManager=io.getCookiesFromURLConnection(urlConnection);
List cookies=cookieManager.getCookieStore().getCookies();
Iterator cookieIterator=cookies.iterator();
while(cookieIterator.hasNext()){
HttpCookie cookie=cookieIterator.next();
cookieString+=cookie.getName()+"="+cookie.getValue()+";";
if(cookie.getName().equals("csrftoken")){
csrftoken=cookie.getValue();
}
}
url= new URL(Utils.USER_SIGNUP_URL);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("User-Agent", Utils.USER_AGENT);
urlConnection.setRequestProperty("Connection", Utils.KEEP_ALIVE);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("X-CSRFToken", csrftoken);
urlConnection.setRequestProperty("Cookies", cookieString);
OutputStreamWriter streamWriter=new OutputStreamWriter(urlConnection.getOutputStream());
streamWriter.write(urlParameters);
streamWriter.flush();
streamWriter.close();
int responseCode=urlConnection.getResponseCode();
response=io.readStream(urlConnection);
} catch (FileNotFoundException ex){
ex.printStackTrace();
try {
response=io.readErrorStream(urlConnection);
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
private class GetData extends AsyncTask {
@Override
protected String doInBackground(Void... params) {
if(checkConnection()){
try {
return fetchData();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}else{
Toast.makeText(SignUpActivity.this, "Please connect to internet", Toast.LENGTH_LONG).show();
}
return "";
}
@Override
protected void onPostExecute(String s) {
if(s==null){
return ;
}
super.onPostExecute(s);
signupButton.setEnabled(true);
}
}
private boolean checkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
String result = "";
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
public String fetchData() throws IOException {
String result="";
try {
url= new URL(Utils.USER_SIGNUP_URL);
urlConnection=(HttpURLConnection)url.openConnection();
result=io.readStream(urlConnection);
cookieManager=io.getCookiesFromURLConnection(urlConnection);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return result;
}
}
以下是io类:
公共类io{
^{pr2}$
Android Logcat:09-19 20:55:37.479 17085-17734/kam.app.learnnetworking W/System.err﹕ java.io.FileNotFoundException: http:///api/user-login/
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at kam.app.learnnetworking.networking.io.readStream(io.java:28)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at kam.app.learnnetworking.activities.SignUpActivity$PostData.doInBackground(SignUpActivity.java:172)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at kam.app.learnnetworking.activities.SignUpActivity$PostData.doInBackground(SignUpActivity.java:123)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-19 20:55:37.484 17085-17734/kam.app.learnnetworking W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
有什么方法可以让我收到发送的请求并在android日志中打印?我刚接触android网络,所以没有太多想法。在