如果TinyURL的API不起作用什么..? [英] What if TinyURL API doesn't work..?

查看:940
本文介绍了如果TinyURL的API不起作用什么..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBScript函数,从一个普通的URL创建TinyURL的。

I have a vbscript function to create a tinyurl from a regular url.

FUNCTION GetShortURL(strUrl)
  Dim oXml,strTinyUrl,strReturnVal
  strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
  set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
  oXml.Open "GET", strTinyUrl, false
  oXml.Send
  strReturnVal = oXml.responseText
  Set oXml = nothing
  GetShortURL = strReturnVal
END FUNCTION

我所遇到的问题时,TinyURL的API已关闭或无法访问,使我的脚本失败:

I have come across the problem when the tinyurl api is down or inaccessible, making my script fail:

MSXML3.DLL
错误'80072efe
与服务器的连接异常终止

有没有保障,我可以添加到这个函数prevent错误并使用长URL它具有..?

Is there a safeguard I can add to this function to prevent the error and use the long url it has..?

在事先非常感谢,

neojak​​ey

neojakey

推荐答案

如果你想只返回 strUrl 如果调用失败,你可以使用的在错误恢复下一步

If you want to just return strUrl if the call fails, you can use On Error Resume Next

FUNCTION GetShortURL(strUrl)
  on error resume next
  Dim oXml,strTinyUrl,strReturnVal
  strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
  set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
  oXml.Open "GET", strTinyUrl, false
  oXml.Send
  strReturnVal = oXml.responseText
  Set oXml = nothing
  'Check if an error occurred.
  if err.number = 0 then
     GetShortURL = strReturnVal
  else
     GetShortURL = strUrl
  end if
END FUNCTION

这篇关于如果TinyURL的API不起作用什么..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆