正在准备一个云盘环境,验证一些细节,为了测试方便,我直接用html调用WebService查看是否正确返回结果。相信很多朋友做过这种简单的事情,由于不熟悉html写法加上自己思维僵化走了点弯路,整理下来给自己备忘。
WebService
@GET
@Path("yyy")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public File getSomthing(
@QueryParam("sthId1") String bucketName,
@QueryParam("sthId2") String objectName
) {
HTML方式
参见:http://msdn.microsoft.com/en-us/library/45fez2a8%28v=vs.80%29.aspx
<form method=POST action='http://localhost:8080/xxx/yyy'>
<input type="text" size="5" name='sthId1'\"></td>
<input type="text" size="5" name='sthId2'\"></td>
<input type=submit value="Try"> </td>
</form>
后边的代码未验证,记录备忘,参见:
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr
调用HelloWorld,无参数
<html>
<head>
<title>Hello World</title>
<script language="JavaScript">
var iCallID;
function InitializeService(){
service.useService(http://localhost:1394/MyWebService.asmx?wsdl,
"HelloWorldService");
service.HelloWorldService.callService("HelloWorld");
}
function ShowResult(){
alert(event.result.value);
}
</script>
</head>
<body onload="InitializeService()" id="service"
style="behavior:url(webservice.htc)" onresult="ShowResult()"> </body>
</html>
调用GetAge,带三个参数
<html>
<head>
<title>UseSwap</title>
<script language="JavaScript">
function InitializeService(){
service.useService(http://localhost:1394/MyWebService.asmx?wsdl,
"GetAgeService");
}
var StrYear, StrMonth, StrDay;
function GetAge(){
StrYear = document.DemoForm.StringYear.value;
StrMonth = document.DemoForm.StringMonth.value;
StrDay = document.DemoForm.StringDay.value;
service.GetAgeService.callService("GetAge", StrYear, StrMonth, StrDay);
}
function ShowResult(){
alert(event.result.value);
}
</script>
</head>
<body onload="InitializeService()" id="service"
style="behavior:url(webservice.htc)" onresult="ShowResult()">
<form name="DemoForm">
Year : <input type="text" name="StringYear"/>
Month : <input type="text" name="StringMonth"/>
Day : <input type="text" name="StringDay"/>
<button onclick="GetAge()">Get Age</button>
</form>
</body>
</html>
返回缓存值调用
<html>
<head>
<meta http-equiv="refresh" content="2" />
<title>Get Date Time</title>
<script language="JavaScript">
var iCallID;
function InitializeService(){
service.useService(http://localhost:1394/MyWebService.asmx?wsdl,
"GetDateTimeService");
service.GetDateTimeService.callService("GetDateTime");
}
function ShowResult(){
alert(event.result.value);
}
</script>
</head>
<body onload="InitializeService()" id="service"
style="behavior:url(webservice.htc)" onresult="ShowResult()">
</body>
</html>