通过POST请求将JSON数据发送到https服务器

时间:2018-02-02 12:06:29

标签: json https http-post json-rpc http-status-code-500

请帮助,也许有人有通过json通过POST请求向服务器发送和接收数据的经验?

通过传递需要传递参数(方法,id,参数)的json数据来获取编程接口(API),并获取会话ID(结果)。

使用库:com.thetransactioncompany.jsonrpc2

问题是,在提交数据的阶段(respIn = JSONRPC2Session.send(reqOut),请求因500错误而下降,但服务器可用,它似乎不明白这是一个发布请求,我的错误是什么?

NetworkException:

Android

这是我的代码" main"

java.io.IOException: Server returned HTTP response code: 500 for URL: https://online.sbis.ru/auth/service/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.thetransactioncompany.jsonrpc2.client.RawResponse.parse(Unknown Source)
    at com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session.send(Unknown Source)
    at ru.documentum.sbis.integration.AuthLink.main(AuthLink.java:115)
Exception in thread "main" java.lang.NullPointerException
    at ru.documentum.sbis.integration.AuthLink.main(AuthLink.java:129)

这是我的课程代码" BasicAuth"

 package ru.documentum.sbis.integration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.Proxy;

import com.thetransactioncompany.jsonrpc2.*;
import com.thetransactioncompany.jsonrpc2.client.*;

import java.io.*;
import java.net.*;
import java.util.*;

import sun.rmi.runtime.Log;
import net.minidev.json.JSONAware;

import com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException;
import com.thetransactioncompany.jsonrpc2.client.ConnectionConfigurator;
import com.thetransactioncompany.jsonrpc2.client.RawResponse;
import com.thetransactioncompany.jsonrpc2.client.RawResponseInspector;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Notification;

public class AuthLink {



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

            URL ServerUrl = null;

        try
            {
                ServerUrl = new URL("https://online.sbis.ru/auth/service/");


            } catch (MalformedURLException e) {

            }  

        // Create new JSON-RPC 2.0 client session
        JSONRPC2Session mySession = new JSONRPC2Session(ServerUrl);
        mySession.getOptions().setRequestContentType("application/json-rpc");
        mySession.getOptions().acceptCookies(true);
        mySession.getOptions().setAllowedResponseContentTypes(new String[]{"application/json-rpc"});
        mySession.getOptions().ignoreVersion(true);
        mySession.getOptions().parseNonStdAttributes(true);
        mySession.getOptions().trustAllCerts(true);

        mySession.setConnectionConfigurator(new BasicAuth());
        mySession.setRawResponseInspector(new MyInspector());

        String method = "СБИС.Аутентифицировать";
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("Логин", "sc6949");
        params.put("Пароль", "!653W74395w");
        String id = "0";

        System.out.println("Creating new request with properties :");
        System.out.println("\tmethod     : " + method);
        System.out.println("\tparams : " + params);
        System.out.println("\tid         : " + id + "\n\n");

        // Create a new JSON-RPC 2.0 request
        JSONRPC2Request reqOut = new JSONRPC2Request(method, params, id);

        // Serialise the request to a JSON-encoded string
        String json = reqOut.toString();        

        System.out.println("reqOut - json: Serialised request to JSON-encoded string :");
        System.out.println("\t" + json + "\n\n");

        JSONRPC2Request reqIn = null;

        try 
            {
                reqIn = JSONRPC2Request.parse(json);

            } catch (JSONRPC2ParseException e) {
            System.out.println(e.getMessage());
            return;
            }
        System.out.println("json - reqIn: Parsed request with properties :");
        System.out.println("\tmethod     : " + reqIn.getMethod());
        System.out.println("\tparams : " + reqIn.getNamedParams());
        System.out.println("\tid         : " + reqIn.getID() + "\n\n");

        String result = "result";

        System.out.println("Creating response with properties : ");
        System.out.println("\tresult : " + result);
        System.out.println("\tid     : " + reqIn.getID() + "\n\n"); // ID must be echoed back

        // Create response
        JSONRPC2Response respOut = new JSONRPC2Response(result, reqIn.getID());

        // Serialise response to JSON-encoded string
        json = respOut.toString();

        System.out.println("Serialised response to JSON-encoded string :");
        System.out.println("\t" + json + "\n\n");

        // Parse response string
        JSONRPC2Response respIn = null;

        try {
            respIn = mySession.send(reqOut);            

                } catch (  JSONRPC2SessionException e) {
            if (e.getCauseType() == JSONRPC2SessionException.NETWORK_EXCEPTION) {
                try {
                    throw e.getCause();
                } catch (Throwable e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
              }
           else     if (e.getCauseType() == JSONRPC2SessionException.BAD_RESPONSE) {
              }
              try {
                throw respIn.getError();
            } catch (JSONRPC2Error e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }

        // Check for success or error
        if (respIn.indicatesSuccess()) {
            System.out.println("getresult The request succeeded :");

            System.out.println("\tresult : " + respIn.getResult());
            System.out.println("\tid     : " + respIn.getID());
        }
        else {
            System.out.println("The request failed :");

            JSONRPC2Error err = respIn.getError();

            System.out.println("\terror.code    : " + err.getCode());
            System.out.println("\terror.message : " + err.getMessage());
            System.out.println("\terror.data    : " + err.getData());
        }
}}

0 个答案:

没有答案