开发者

How to get response from web API call

I have an API that I want to call and get the response back. Then I want to assign that response to a variable.

I have an API like:

http://example.org/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx

When I run this URL in a browser, it prints an SMS balance. I want this SMS balance as a response and then assign it to a variable开发者_Python百科 in my page.


You may also use WebRequest class.

WebRequest request = WebRequest.Create ("http://domainname/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx");

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();

Of course, you can change Console.WriteLine to whatever you want to do with response.


Your service is an .aspx page which just returns text (the sms balance) and no html? If so you can 'scrape it'

string urlData = String.Empty;
WebClient wc = new WebClient();
urlData = wc.DownloadString("http://domainname/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜