【问题标题】:Sending JSON POST to Django App将 JSON POST 发送到 Django 应用程序
【发布时间】:2012-08-04 20:36:15
【问题描述】:

我有一些 Java 代码尝试将 POST 数据发送到 Django 应用程序。但是,视图根本不会被调用。如果我将 java 代码命中的相同 URL 粘贴到我的浏览器中,则会调用 Django 视图。我不知道我错过了什么,但 Java 编写一定有问题。

这是执行写入操作的 Java 函数:

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 直接对应于Django 视图的requestURL。 requestURL 是:

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

这是 Django Urlconfig:

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

最后,这是 Java 代码永远不会调用的视图

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

那么,我做错了什么,Django 服务器从未收到过 POST 请求?我花了大约 2 个小时试图弄清楚,我找不到 Java 代码的任何问题。不过显然有些地方出了问题。

【问题讨论】:

  • 您应该测试 Java 应用程序发送的确切内容并在此处显示以了解问题出在 Java 部分还是 Django。
  • 你建议如何测试它?我试图让 Wireshark 向我展示流量,但我似乎无法做到这一点。我在 OS X 上。它向我显示了大量其他 HTTP 流量,但由于某种原因,它甚至没有记录我的工作 REST 调用。
  • 你看到开发服务器上有请求吗?检查您没有在视图中多次定义相同的函数。
  • @Jon 你可以使用netcat监听8000端口并停止django服务器。数据包被打印到控制台。在 Linux 中的语法是 nc -l -p 8000

标签: java django json rest


【解决方案1】:

确保您的视图是 csrf exempt,因为您没有从 Java 请求中发送适当的 CSRF 令牌。

【讨论】:

  • 我进行了更改,但错误仍然存​​在。不过,这无疑是朝着正确方向迈出的一步。
【解决方案2】:

我认为 crsf 实际上是问题所在。添加后,我稍微更改了 Java 代码,它就可以工作了。我仍然不确定细微的 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();
    }
}

【讨论】:

    【解决方案3】:

    我记得使用“POST”类型时需要将 URL 更改为“http://127.0.0.1:8000/events/rest/33456/create/”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2012-04-15
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多