在c#.net中调用API网址 [英] Call API Url in c#.net

查看:50
本文介绍了在c#.net中调用API网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我想在C#.net中调用我的API链接(URL)
我正在使用System.Process(URL);
但这是在浏览器中打开的,我不想打开浏览器.
那我怎么称呼它呢?
请给我个主意...

谢谢

Hello Friends,

I want to call My API Link(URL) in C#.net
I is working using System.Process(URL);
But it is open in the Browser, I don''t want to open browser.
so how can i call it ?
Please Give me idea...

Thanks

推荐答案

string apiUrl ="YourAPIURL";

Uri地址=新Uri(apiUrl);

//创建Web请求
HttpWebRequest request = WebRequest.Create(address)as HttpWebRequest;

//将类型设置为POST
request.Method ="GET";
request.ContentType ="text/xml";

使用(HttpWebResponse response = request.GetResponse()作为HttpWebResponse)
{
//获取响应流
StreamReader reader =新的StreamReader(response.GetResponseStream());

//控制台应用程序输出
字符串strOutputXml = reader.ReadToEnd();
}
string apiUrl = "YourAPIURL";

Uri address = new Uri(apiUrl);

// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

// Set type to POST
request.Method = "GET";
request.ContentType = "text/xml";

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());

// Console application output
string strOutputXml = reader.ReadToEnd();
}


如果要从URL获取HTML代码并自行处理.
使用可以使用HttpWebRequest来获取所有文本.

If you want to get the HTML code from a URL and process it your self.
Use can use HttpWebRequest to get all text.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
  Stream receiveStream = response.GetResponseStream();
  StreamReader readStream = null;

  if (response.CharacterSet == null)
    readStream = new StreamReader(receiveStream);
  else
    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));

  string data = readStream.ReadToEnd();
  response.Close();
  readStream.Close();
}



并且所有文本都将在字符串类data



And all text will be in string class data


这篇关于在c#.net中调用API网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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