Https Connection使用身份验证和发送POST请求查询并获取JSON输出

时间:2015-08-04 16:17:47

标签: java post https httpsurlconnection

我正在编写一个程序,该程序将连接到Https url并将使用用户名和密码进行身份验证,并在Json中发送Post请求Query并获取输出。我一直被困在身份验证部分并发送Post请求。请帮帮我。如何以及在何处添加用户名密码?

另外我需要添加headers =(“application / json”)

public static void main(String a[]) throws IOException{


            URL url = new URL("https://url");
            URLConnection connection = url.openConnection();
            connection.connect();
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
            httpsConnection.setAllowUserInteraction(true);



         StringEntity input = new StringEntity("{'query': 'select i.interfaceid, i.node.caption as node_name, i.node.nodeid as nodeid, i.caption as interface_name, i.interfacelastchange, i.operstatus, i.adminstatus from orion.npm.interfaces i where (i.unmanaged = 0 and i.status <> 1) and daydiff(i.interfacelastchange, getdate()) >= 90 and (i.operstatus > 1 or i.adminstatus > 1)', 'parameters': ''}");

 request.setRequestMethod("POST");
 OutputStream post = request.getOutputStream();
 input.writeTo(post);
 post.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
 String inputLine, response = "";
 while ((inputLine = in.readLine()) != null) {
  response += inputLine;
 }
 post.close();
}

1 个答案:

答案 0 :(得分:0)

public string ScrubHtml(string value)
        {
            var step1 = System.Text.RegularExpressions.Regex.Replace(value, @"<[^>]+>|&nbsp;", "").Trim();
            var Message_ = System.Text.RegularExpressions.Regex.Replace(step1, @"\s{2,}", " ");
            return Message_;
        }