ThingWorx : 2、使用Java Tcp Client向服务器发送数据

本文介绍如何配置ThingWorx平台以接受REST API请求,并提供了一个Java示例程序来发送POST请求到ThingWorx服务器。该程序通过Socket连接发送数据,并接收服务器返回的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 设置TingWorx可接受Rest API

经过设置后,服务器就能接受HTTP1.1的数据
这里写图片描述
选择Allow Request Method Switch
不选Filter Content-Type
这里写图片描述
设置为如下保存即可接收Rest API
这里写图片描述

2. 编写java测试软件

package ptcTest;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class PtcTest {

    private static void debug(String s)
    {
        System.out.println(s);

    }

    static final String appKey = "8498cdb8-c245-485f-96ff-de108ec74fc2";
    static final String thingName = "eHealthThing";
    static final String serviceName = "eHealthService";
    static final String server = "ge2-4022.cloud.thingworx.com";

    public static void main(String[] argvs)
    {
        debug("main");

        //建立Socket  
        Socket s;
        try {
                s = new Socket(server,80);
                InputStream ips=s.getInputStream();  
                OutputStream ops=s.getOutputStream();  

                DataOutputStream dos = new DataOutputStream(ops);  
                BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));  

                int value = 123;
                while(true)
                {

                    String data = "?appKey="
                                +appKey
                                +"&method=post&x-thingworx-session=true";

                    String post = "POST /Thingworx/Things/"
                                +thingName
                                +"/Services/"
                                +serviceName
                                +data
                                +"&"
                                +"heartBeat"
                                +"="
                                +value
                                +"&"
                                +"location"
                                +"="
                                +"11,22"                                        
                                +" HTTP/1.1\r\n"
                                +"Host: "
                                +server+"\r\n"
                                +"Content-Type: text/html\r\n"
                                +"\r\n";

                        dos.writeBytes(post);
                        debug("seend\r\n"+post);

                        while(true)
                        {
                            String strWord = brNet.readLine(); 
                            if(strWord!=null)
                                debug(strWord);
                        }
                    }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

请自行替换以下的参数

    static final String appKey = "8498cdb8-c245-485f-96ff-de108ec74fc2";
    static final String thingName = "eHealthThing";
    static final String serviceName = "eHealthService";
    static final String server = "ge2-4022.cloud.thingworx.com";

运行软件后打印信息如下

main
seend
POST /Thingworx/Things/eHealthThing/Services/eHealthService?appKey=2e2f8779-5d0d-4929-8260-acc65cefb5f9&method=post&x-thingworx-session=true&heartBeat=123&location=11,22 HTTP/1.1
Host: ge2-5678.cloud.thingworx.com
Content-Type: text/html


HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=D0230FE07DB4F8CC9CFB3E9B1AFC2796; Path=/Thingworx/; HttpOnly
Expires: 0
Cache-Control: no-store, no-cache
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 03 Mar 2016 03:17:51 GMT

0

服务器已经返回了正确的信息

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值