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
服务器已经返回了正确的信息