1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package com.hollysys.hiadsp.websocket.test;
import java.util.UUID;
import com.hollysys.hiadsp.eventhub.util.HttpClientUtils;
import net.sf.json.JSONObject; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient;
public class PublishClientTest { public static void main(String[] args) { String url;
url = "http://workflow-eventhub-test-hollysys-project.hiadev.tk:80/eventhub/rest/send"; String uuid = UUID.randomUUID().toString(); String body_json;
String tenant_id = "000000"; String topicName = "sssssss"; body_json = "{\"eventName\": \"" + topicName + "\",\"eventTime\": 1517985178794,\"source\": \"\",\"sourceProperty\": \"\",\"eventData\": {\"tntId\":\"" + tenant_id + "\",\"sesssiong \":\"这是测试信息!!!!\"}}"; body_json="{\"eventName\":\"sssssss\",\"eventTime\":\"1517985178794\",\"source\":\"\",\"sourceProperty\":\"\",\"eventData\":{\"tntId\":\"000000\",\"modelKey \":\"haier_temperature_ridge\",\"predictionData\":{\"data\":[]}}}"; JSONObject json = JSONObject.fromObject(body_json); System.out.println(json); JSONObject resultJson=null; int i=0; while (true){ try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } resultJson = doPost(url, json); System.out.println("消息"+i+"发送成功! " + resultJson.toString()); i++; }
} public static JSONObject doPost(String url, JSONObject json) { DefaultHttpClient client = new DefaultHttpClient(); String surl = url.replaceAll(" ", ""); HttpPost post = new HttpPost(surl); JSONObject response = null; try { StringEntity s = new StringEntity(json.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); post.setEntity(s); HttpResponse res = client.execute(post); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(res.getEntity()); response = JSONObject.fromObject(result); } } catch (Exception e) { throw new RuntimeException(e); } return response; } }
|