HY,我会将json发送到网关
我可以使用以下代码执行登录网关:
private static final String SPN_IP = "192.168.0.2";
private static final String SPN_LOGIN = "myadmin";
private static final String SPN_PASSWORD = "mypasswor";
private static String jsonSicuro = "[\"txmsgid\":,\"trycount\": 5,\"port\": 2,\"payload\": \"01\",\"ack\": true,\"mote\": \"17A7F79\" }]";
public static void main(String[] args) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, AuthenticationException, URISyntaxException {
// SSL management
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); // trust self-signed cert
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
return SPN_IP.equals(hostname); // validate the host name
}
});
// Create client
CloseableHttpClient httpclient = HttpClientBuilder.create()
.setSSLSocketFactory(sslSocketFactory)
.build();
// Create HTTP GET request
HttpGet httpGet = new HttpGet("https://" + SPN_IP);
// HTTP digest
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("algorithm", "MD5");
digestAuth.overrideParamter("realm", "https://" + SPN_IP);
digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
digestAuth.overrideParamter("qop", "auth");
digestAuth.overrideParamter("nc", "0");
digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());
// Send HTTP digest header
Header auth = (Header) digestAuth.authenticate(new UsernamePasswordCredentials(SPN_LOGIN, SPN_PASSWORD), httpGet, null);
System.out.println(((org.apache.http.Header) auth).getName());
System.out.println(((org.apache.http.Header) auth).getValue());
httpGet.setHeader((org.apache.http.Header) auth);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
System.out.println("Executing request: " + httpGet.getRequestLine());
String response = httpclient.execute(httpGet, responseHandler);
System.out.println("Response: " + response);
网关响应“授权确定”..................... 现在,我该如何发送json?我尝试“httpGet.setParams((HttpParams)((HttpEntity)new StringEntity(jsonSicuro)));”但我有错误
答案 0 :(得分:0)
我们无法将json发送到httpGet,您只能将请求正文发送到httpPost。 请检查 What is the difference between POST and GET?
另请查看以下链接以传递参数 commons httpclient - Adding query string parameters to GET/POST request