.net的web service服务跟ruby有点区别,不过可以使用post方式来使用,这个很方便。
一般来说.net提供的服务支持GET,POST和SOAP方式,还提供了一个简单的网页以供测试,先测试好服务是可用的,然后写ruby客户端。
res=Net::HTTP.post_form(URI.parse(SERVICE_POST_URL),{...parameters...}) #参数是一个hash,由服务来决定接口参数
#处理返回结果
case res
when Net::HTTPSuccess
doc=Hpricot.parse(res.body)
ret_msg=(doc/:string).text
if error_msg?(ret_msg)
return {"error"=>"authentication error!"}
else
return {"msg"=>ret_msg}
end
when Net::HTTPInternalServerError
return {"error"=>"internal server error!"}
when Net::HTTPForbidden
return {"error"=>"service forbidden"}
when Net::HTTPNotFound
return {"error"=>"service no found!"}
when Net::HTTPServiceUnavailable
return {"error"=>"service unavailable!"}
else
return {"error"=>"err!"}
end
其中Hpricot是解析xml和html的,一般会返回xml,解析起来还算快,api也特别简单。各种case就是来判断返回错误的。
直接用soap包似乎有点问题,对返回的http 头处理不了。而.net会返回一个头,不是直接返回xml的。
一般来说.net提供的服务支持GET,POST和SOAP方式,还提供了一个简单的网页以供测试,先测试好服务是可用的,然后写ruby客户端。
res=Net::HTTP.post_form(URI.parse(SERVICE_POST_URL),{...parameters...}) #参数是一个hash,由服务来决定接口参数
#处理返回结果
case res
when Net::HTTPSuccess
doc=Hpricot.parse(res.body)
ret_msg=(doc/:string).text
if error_msg?(ret_msg)
return {"error"=>"authentication error!"}
else
return {"msg"=>ret_msg}
end
when Net::HTTPInternalServerError
return {"error"=>"internal server error!"}
when Net::HTTPForbidden
return {"error"=>"service forbidden"}
when Net::HTTPNotFound
return {"error"=>"service no found!"}
when Net::HTTPServiceUnavailable
return {"error"=>"service unavailable!"}
else
return {"error"=>"err!"}
end
其中Hpricot是解析xml和html的,一般会返回xml,解析起来还算快,api也特别简单。各种case就是来判断返回错误的。
直接用soap包似乎有点问题,对返回的http 头处理不了。而.net会返回一个头,不是直接返回xml的。