新手上路,最近经常用到调用api接口的情况,作为一个新手在遇到这种情况时还是有些方,不过现在网上大神众多只要肯找,答案还是有无限多的,下面分享下我总结的c#常用到的访问方式,希望能有点帮助,欢迎各位大神指点!
一、使用HttpWebRequest类访问api
get访问方式
1 public static string HttpRequestToGET(string path) 2 { 3 try 4 { 5 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(path); 6 7 //配置webRequest 8 myRequest.Method = "GET"; 9 myRequest.ContentType = "application/x-www-form-urlencoded"; 10 11 //myRequest.ContentLength = postdata.Length; 12 13 using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse()) 14 { 15 var sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 16 var responseString = sr.ReadToEnd(); 17 18 if (!string.IsNullOrEmpty(responseString)) 19 { 20 return responseString; 21 } 22 } 23 } 24 catch (Exception ex) 25 { 26 throw new Exception("网络连接失败,请尝试重新启动系统!或联系运营人员"); 27 } 28 29 return ""; 30 }
post方式访问
1 public static string HttpRequestToPOST(string path, string json) 2 { 3 try 4 { 5 //组装地址 6 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(path); 7 8 //配置webRequest 9 myRequest.Method = "POST"; 10 myRequest.ContentType = "application/json"; 11 byte[] byteData = Encoding.UTF8.GetBytes(json); 12 myRequest.ContentLength = byteData.Length; 13 Stream stream = myRequest.GetRequestStream(); 14 stream.Write(byteData, 0, byteData.Length); 15 stream.Close(); 16 //myRequest.ContentLength = postdata.Length; 17 18 using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse()) 19 { 20 var sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 21 var responseString = sr.ReadToEnd(); 22 23 if (!string.IsNullOrEmpty(responseString)) 24 { 25 return responseString; 26 } 27 } 28 } 29 catch (Exception ex) 30 { 31 throw new Exception("网络连接失败,请尝试重新启动系统!或联系运营人员"); 32 } 33 34 return ""; 35 }
put方式
1 public static string HttpRequestToPUT(string path, string json) 2 { 3 try 4 { 5 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(path); 6 7 //配置webRequest 8 myRequest.Method = "put"; 9 myRequest.Credentials = CredentialCache.DefaultCredentials; 10 myRequest.ContentType = "application/json"; 11 myRequest.Accept = "application/json"; 12 byte[] byteData = Encoding.UTF8.GetBytes(json); 13 myRequest.ContentLength = byteData.Length; 14 Stream stream = myRequest.GetRequestStream(); 15 stream.Write(byteData, 0, byteData.Length); 16 stream.Close(); 17 18 using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse()) 19 { 20 var sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 21 var responseString = sr.ReadToEnd(); 22 23 if (!string.IsNullOrEmpty(responseString)) 24 { 25 return responseString; 26 } 27 } 28 } 29 catch (Exception ex) 30 { 31 //throw new Exception("网络连接失败,请尝试重新启动系统!或联系运营人员"); 32 throw ex; 33 } 34 35 return ""; 36 }
del方式访问
1 public static string HttpRequestToDEL(string path) 2 { 3 try 4 { 5 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(path); 6 7 //配置webRequest 8 //myRequest.Method = "DELETE"; 9 myRequest.Method = "DELETE"; 10 myRequest.Credentials = CredentialCache.DefaultCredentials; 11 myRequest.ContentType = "application/json"; 12 myRequest.Accept = "application/json"; 13 //myRequest.ContentLength = postdata.Length; 14 15 using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse()) 16 { 17 var sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 18 var responseString = sr.ReadToEnd(); 19 20 if (!string.IsNullOrEmpty(responseString)) 21 { 22 return responseString; 23 } 24 } 25 } 26 catch (Exception ex) 27 { 28 throw new Exception("网络连接失败,请尝试重新启动系统!或联系运营人员"); 29 } 30 31 return ""; 32 }
二、使用WebClient类来进行接口访问
get方式
public string GetMothedApi(string pro)
{
//实例化
WebClient client = new WebClient();
//地址
string path = "http://oa.bbs.com/bbs/L?pro=" + pro;
//上传并接收返回数据(这是同步,需要等待接收返回值)
byte[] responseData = client.DownloadData(path);
//释放
client.Dispose();
//处理返回数据(一般用json)
string srcString = Encoding.UTF8.GetString(responseData);
return srcString;
}
post方式
public string PostMothedApi(string pro, string ids)
{
//实例化
WebClient client = new WebClient();
//地址
string path = "http://oa.bbs.com/bbs/L?pro=" + pro;
//数据较大的参数
string datastr = ids;
//参数转流
byte[] bytearray = Encoding.UTF8.GetBytes(datastr);
//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//长度
client.Headers.Add("ContentLength", bytearray.Length.ToString());
//上传,post方式,并接收返回数据(这是同步,需要等待接收返回值)
byte[] responseData = client.UploadData(path, "POST", bytearray);
//释放
client.Dispose();
//处理返回数据(一般用json)
string srcString = Encoding.UTF8.GetString(responseData);
return srcString;
}