try {
// create HttpPost and HttpClient object
if (post == null){
post = new HttpPost(apiurl);
client = HttpClientBuilder.create().build();
}
// setup output message by copying JSON body into
// apache StringEntity object along with content type
entity = new StringEntity(jsonBody, HTTP.UTF_8);
entity.setContentEncoding(HTTP.UTF_8);
entity.setContentType("text/json");
// add HTTP headers
post.setHeader("Accept", "text/json");
post.setHeader("Accept-Charset", "UTF-8");
// set Authorization header based on the API key
post.setHeader("Authorization", ("Bearer "+apikey));
post.setEntity(entity);
post.setHeader(HTTP.CONN_DIRECTIVE,HTTP.CONN_KEEP_ALIVE);
System.out.println();
HttpResponse authResponse=null;
long t1 = System.currentTimeMillis();
// Call REST API and retrieve response content
authResponse = client.execute(post);
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
//}
}
catch (Exception e) {
return e.toString();
}
3 回答
实际上,没有必要使用教程等 . 在Workplace的WebService窗格中,选择您的Web服务,然后,在其中,您应该拥有为Web服务设置的完整代码示例 .
实用页面:https://azure.microsoft.com/en-us/documentation/articles/machine-learning-consume-web-services/
https://azure.microsoft.com/en-us/documentation/articles/machine-learning-consume-web-service-with-web-app-template/
有两个指南文章,因为示例可以帮助您如何入门,而不是代码示例 .
Connect to an Azure Machine Learning Web Service
Consume an Azure Machine Learning web service with a web app template
希望能帮助到你 .
这是示例java代码 - https://github.com/nk773/AzureML_RRSApp . 这需要Apache HTTP client