Wolfram 语言

云端和网络界面

空间站跟踪网页响应控制

通过返回一个 HTTPResponse 可以从一个云对象控制 HTTP 响应的确切特征. 错误情形可用 HTTPErrorResponse 处理.

定义函数,找到离国际空间站当前位置最近的城市. 空间站移动迅速,因而结果也快速变化.

显示完整的 Wolfram 语言输入
In[1]:=
Click for copyable input
issLocationDisplay[] := Module[{pos, posline, country, city, distance}, {pos, posline} = SatelliteData[ Entity["Satellite", "25544"], {"Position", "PositionLine"}]; country = First[GeoNearest["Country", pos]]; distance = Round[GeoDistance[pos, country], 0.1]; Panel@Column[{Row[{"ISS Position: ", Style[If[distance <= Quantity[0., "Miles"], "over " <> First[GeoNearest["City", pos]]["Name"] <> ", " <> country["Name"], ToString[ Row[{"over the open sea ", distance, " from ", country["Name"]}]]], Darker[Blue]]}, BaseStyle -> Directive["FontFamily" -> "SansSerif", RGBColor["#497CA4"], Larger, Bold]], GeoGraphics[{Gray, Thickness[.005], Arrowheads[{{0.05, 0.4}, {0.05, 0.13}}], Arrow @@ posline, Red, PointSize[.01], Point[pos], Opacity[.1], Black, GeoVisibleRegion[pos]}, GeoCenter -> pos, GeoRange -> "World", ImageSize -> 600]}]]
In[2]:=
Click for copyable input
issLocationDisplay[]
Out[2]=

部署每次都作计算的云对象,用 HTTPResponse 指出内容类型,并指定一个头,告诉浏览器不要缓存结果.

In[3]:=
Click for copyable input
obj = CloudDeploy[ Delayed[HTTPResponse[ ExportString[issLocationDisplay[], "PNG"], <| "ContentType" -> "image/png", "Headers" -> {"Cache-Control" -> "no-cache"}|>]], CloudObject["iss-location"], Permissions -> "Public"]
Out[3]=

禁止某些用户使用云对象,返回 HTTPErrorResponse.

In[4]:=
Click for copyable input
obj = CloudDeploy[Delayed[ If[MemberQ[{"badapple@example.com", "blackhat@example.com", "spammer@example.com"}, $WolframID], HTTPErrorResponse[403], HTTPResponse[ issLocationDisplay[], <| "Headers" -> {"Cache-Control" -> "no-cache"}|>] ]], CloudObject["iss-location"], Permissions -> "Public"]
Out[4]=

相关范例

de en es fr ja ko pt-br ru