I write a piece of java code to log on to the website that I deployed with the following code. But it gives a 403 error
[POST / accounts / login / HTTP / 1.1] 403 2282
can anyone how to solve it Gets the idea? Thanks a lot before!
Private string base = "http: // localhost: 9999"; Boolean result = false; {URL url = Try new URL (baseUrl + "/ accounts / login /"); HttpURLConnection con = (HttpURLConnection) url.openConnection (); Con.setRequestMethod ("post"); Con.setDoInput (true); Con.setDoOutput (right); String FormParameters = "Csrfmiddlewaretoken =" + para + "& amp; username =" + "name" + "& amp; password =" + "pwd" + "& amp; next ="; Println (formParameters); DataOutputStream wr = New DataOutputStream (con.getOutputStream ()); Wr.writebytes (URLEncoder.encode (form parameter, "UTF-8")); Wr.flush (); Wr.close (); If (con.getResponseCode () == 302) {result = true; }} Hold (malformedULException e) {// todo automatically generated blocking block e.printStackTrace (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); } Return results;
Hey I have the same problem for a while now was troubleshooting and I think I need to share it. It's probably too late, but in the future all those people that need some help with a similar issue can be useful. Request Access Page and Django Cookie will be set. Note : Your header value As you can see that there is a CSRF cookie in it, now you need to do two more things: The previous cookie will be sent to your next url Keep in anger properties You Token value your CSRF in your post standards (in this example "FwYSncufKaCZjxLWGUPq7 ORZRvTXIxkU", you can get the price of) Here is some code : Hope this will be helpful for all people in the future!
URL URL = new URL ("http: // localhost: 9999"); HttpURLConnection Client = (HttpURLConnection) url.openConnection (); Client.setDoInput (true); Client.connect (); Inputstream = client.getInputStream (); // open input stream // input stringworld = new stringwriter (); IOUtils.copy (inputstream, author, "UTF-8"); String string = writer.strusting (); // Note (see under this code section) string cookie = client.getHeaderField ("set-cookie"). Get (0); Client.disconnect ();
is stored HashMap & lt, string, List & lt; String & gt; & Gt;
, I used the get (0)
because in my list there is only 1 value in the list for "set-cookie", so I get the index 0 in this example it is included is: "csrftoken = FwYSncufKaCZjxLWGUPq7ORZRvTXIxkU; expires = Sun, 09 Julai 2017 18:23:09 GMT; maximum age = 31449600; path = / 1"
url = new URL (LOGIN_URL); Client = (HttpURLConnection) url.openConnection (); Client.setDoInput (true); Client.setDoOutput (true); Client.setRequestMethod ("post"); // Set the cookie in a previous cookie that has the correct CSRF token client .setRequestProperty ("cookie", cookie); // Set the form parameter for the post string form parameter = "csrfmiddletoken =" + csrf + "& user name =" + "name" + "and password =" + "PWD"; DataOutputStream wr = New DataOutputStream (client.getOutputStream ()); Wr.write (formParameters.getBytes ("UTF-8")); Wr.flush (); Wr.close (); Client.connect (); Inputstream = client.getInputStream (); // Read the return input from the DNS server author = new stringwriter (); IOUtils.copy (inputstream, author, "UTF-8"); String = author.tastring ();
Comments
Post a Comment