简体   繁体   English

如何使用Java将HTTP POST请求发送到Opbeat?

[英]How to send an HTTP POST Request to Opbeat using Java?

I am trying to send an HTTP POST request using java. 我正在尝试使用Java发送HTTP POST请求。 I was able to send a simple json message but when I try to add multiple properties to the json message I get a 400 bad request response. 我能够发送一条简单的json消息,但是当我尝试向json消息添加多个属性时,我收到了400错误的请求响应。 I am attempting to use Gson to_json in order to properly display it. 我正在尝试使用Gson to_json来正确显示它。 Does anyone see where I am going wrong? 有人看到我要去哪里了吗?

public boolean Record(String message, LevelType levelType)
{
    try
    {

        URL url;
        url = new URL(String.format("https://intake.opbeat.com/api/v1/organizations/%s/apps/%s/errors/", this.OrganizationId, this.AppId));
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestProperty  ("Authorization", "Bearer " + this.SecretToken);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        //write parameters
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        JsonObject test = new JsonObject();
        test.addProperty("Application ", ApplicationName);
        test.addProperty("Environment ", environment.name());
        test.addProperty("error ", message);
       String testAsJson = new Gson().toJson(test);
        writer.write(testAsJson);
        writer.flush();
        writer.close();

        // Get the response
        int responseCode = conn.getResponseCode();
        StringBuffer answer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null)
        {
            answer.append(line);
        }
        reader.close();
        //Output the response
        CustomLog.printlnVerbose(answer.toString());
    }
    catch (MalformedURLException ex)
    {
        CustomLog.println(ex);
    }
    catch (IOException ex)
    {
        CustomLog.println(ex);
    }
    //Send an http post to opbeat
    return true;
}

} }

Please save this file on your project: 请将此文件保存在您的项目中:

jetty-web.xml jetty-web.xml

 <?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure id="WebAppContext" class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="maxFormContentSize" type="int">800000</Set> </Configure> 
Put location your project: ..../web/WEB-INF/jetty-web.xml 放置您的项目的位置:.... / web / WEB-INF / jetty-web.xml

maxFormContentSize = set your size who many you want. maxFormContentSize =设置您想要的大小。

I figured it out. 我想到了。 I needed to do a couple things. 我需要做几件事。

  1. change "writer.write(testAsJson)" to "writer.write(testAsJson.to_string)" 将“ writer.write(testAsJson)”更改为“ writer.write(testAsJson.to_string)”

  2. When adding a second json object , instead of "testAsJson.addProperty" I needed to use "testAsJson.add". 当添加第二个json对象时,我需要使用“ testAsJson.add”而不是“ testAsJson.addProperty”。

That resolved my errors and successfully completed the POST request. 那解决了我的错误并成功完成了POST请求。 I really appreciate the answers posted though! 我非常感谢发布的答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM