2012-08-04 2 views
0

POST 코드를 D 장고 응용 프로그램으로 보내려고하는 Java 코드가 있습니다. 그러나보기는 단순히 호출되지 않습니다. 자바 코드가 내 브라우저에 도착하는 동일한 URL을 붙여 넣으면 장고보기가 호출됩니다. 내가 무엇을 놓치고 있는지 전혀 알지 못하지만, 자바가 쓰는 것이 틀림 없다.JSON POST를 Django App으로 보내기

이 쓰기를하고있는 자바의 기능은 다음과 같습니다

public void executeWrite(String requestUrl, JsonObject jsonObject) 
{ 
    DataInputStream input = null; 
    try 
    { 
     URL     url; 
     HttpURLConnection urlConn; 
     DataOutputStream printout; 

     System.out.println(requestUrl); 
     // URL of CGI-Bin script. 
     url = new URL (requestUrl); 
     // URL connection channel. 
     urlConn = (HttpURLConnection)url.openConnection(); 
     // Let the run-time system (RTS) know that we want input. 
     urlConn.setDoInput (true); 
     // Let the RTS know that we want to do output. 
     urlConn.setDoOutput (true); 
     // No caching, we want the real thing. 
     urlConn.setUseCaches (false); 
     // Specify the content type. 
     urlConn.setRequestMethod("POST"); 
     urlConn.setRequestProperty("content-type","application/json; charset=utf-8"); 

     OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream()); 
     wr.write(jsonObject.toString()); 
     wr.flush(); 
     wr.close(); 
    } 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 

는 이제 requestURL 함수가 직접 장고 뷰의 하나에 해당에 전달. requestURL은 마지막으로이 내가 뭘

@csrf_exempt 
def restCreateEvent(request, key): 
    #doesn't really matter what is in here it never runs 

그래서 자바 코드에 의해 호출 결코 극복 뷰입니다

(r'^events/rest/(?P<key>\d+)/create', 'events.views.restCreateEvent'), 

:

http://127.0.0.1:8000/events/rest/33456/create 

이 장고 Urlconfig입니다 장고 서버가 POST 요청을받지 못했다고 잘못 생각합니까? 나는 그것을 이해하려고 약 2 시간을 보냈고 자바 코드에 대해서는 어떤 문제도 찾을 수 없다. 분명히 뭔가 잘못되었습니다.

+0

Java 응용 프로그램이 전송 한 내용을 정확히 테스트하고 문제가 Java 부분 또는 장고에 있는지 알아야합니다. – Lycha

+0

테스트하는 것이 어떻습니까? 나는 Wireshark에 나에게 교통량을 보여 주려고했지만, 그렇게 할 수는 없었다. 나는 OS X에있다. 그것은 나에게 다른 HTTP 트래픽의 톤을 보여 주지만, 심지어 내 작업 REST 호출은 어떤 이유로 그것으로 기록되지 않는다. – Jon

+0

개발 서버에 요청이 오는 것을 보시겠습니까? 뷰에서 동일한 함수의 다중 정의를 확인하지 마십시오. – Rohan

답변

1

Java 요청에서 적절한 CSRF 토큰을 보내지 않으므로보기가 csrf exempt인지 확인하십시오.

+0

으로 변경했지만 오류는 계속 발생합니다. 그것은 확실히 올바른 방향으로 나아가는 단계입니다. – Jon

0

저는 crsf 일이 실제로 문제라고 생각합니다. 일단 내가 자바 코드를 약간 바꿨다 고 덧붙였다. 미묘한 Java 오류가 무엇인지 아직 잘 모르겠습니다. 여기에 Java 코드가 있습니다.

public void executeWrite(String requestUrl, JsonObject jsonObject) 
{ 
    InputStreamReader input = null; 
    try 
    { 
     URL     url; 
     HttpURLConnection urlConn; 
     DataOutputStream printout; 

     System.out.println(requestUrl); 
     // URL of CGI-Bin script. 
     url = new URL (requestUrl); 
     // URL connection channel. 
     urlConn = (HttpURLConnection)url.openConnection(); 
     // Let the run-time system (RTS) know that we want input. 
     urlConn.setDoInput (true); 
     // Let the RTS know that we want to do output. 
     urlConn.setDoOutput (true); 
     // No caching, we want the real thing. 
     urlConn.setUseCaches (false); 
     // Specify the content type. 
     urlConn.setRequestMethod("POST"); 
     urlConn.setRequestProperty("content-type","application/json; charset=utf-8"); 

     OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream()); 
     wr.write(jsonObject.toString()); 
     wr.flush(); 
     wr.close(); 

     input = new InputStreamReader (urlConn.getInputStream()); 
     String response = UserInterface.read(new BufferedReader(input)); 

     if(response.length() > 0) 
     { 
      System.out.println("Response:" + response); 
     } 

     input.close(); 
    } 
    catch(IOException ex) 
    { 
     ex.printStackTrace(); 
    } 
} 
관련 문제