.NET接入接口/请求服务器
之前只调用过自己写的接口,这个是调用外部接口
一.创建方法链接接口
1 public static string HttpWebRequest(string url, string data, Encoding encode, string contentType = "application/x-www-form-urlencoded", CookieContainer cookieContainer = null, string UserAgent = null, WebProxy proxy = null, int timeOut = 60000, string method = "Get", string token = null) 2 { 3 if (string.IsNullOrEmpty(url)) { return string.Empty; } 4 string result = string.Empty; 5 System.Net.HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;//创建HttpWebRequest 6 try 7 { 8 webRequest.Method = method;//传输方式Get/Post 9 webRequest.Timeout = timeOut;/获取或设置 GetResponse 和 GetRequestStream 方法的超时值(以毫秒为单位)。 10 webRequest.ContentType = contentType;//内容类型 11 webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";//获取或设置 Accept HTTP 标头的值。 12 webRequest.KeepAlive = true; 13 webRequest.UserAgent = UserAgent; 14 15 //****************** 16 //添加头,一般用于接口方验证 17 webRequest.Headers.Add("UserName", "TingZi Unique"); 18 //****************** 19 20 webRequest.ContentLength = 0; //内容长度 21 if (!string.IsNullOrWhiteSpace(token)) 22 webRequest.Headers.Add("X-Api-Auth", token); 23 if (proxy != null) 24 webRequest.Proxy = proxy; 25 if (cookieContainer != null) 26 { 27 webRequest.CookieContainer = cookieContainer; 28 } 29 if (!string.IsNullOrEmpty(data)) 30 { 31 byte[] buffer = encode.GetBytes(data); // 转化 32 webRequest.ContentLength = buffer.Length; 33 //写入提交数据 34 using (System.IO.Stream newStream = webRequest.GetRequestStream()) 35 { 36 newStream.Write(buffer, 0, buffer.Length); //写入参数 37 newStream.Flush(); 38 } 39 } 40 41 //提交请求 42 using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse()) 43 { 44 if (cookieContainer != null) 45 { 46 response.Cookies = cookieContainer.GetCookies(response.ResponseUri); 47 } 48 //判断是否返回的是压缩信息 49 if (response.ContentEncoding.ToLower().Contains("gzip")) 50 { 51 using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) 52 { 53 using (StreamReader sreader = new StreamReader(stream)) 54 { 55 result = sreader.ReadToEnd(); 56 } 57 } 58 } 59 else 60 { 61 using (Stream stream = response.GetResponseStream()) 62 { 63 using (StreamReader reader = new StreamReader(stream, encode)) 64 { 65 result = reader.ReadToEnd(); 66 } 67 } 68 } 69 } 70 } 71 catch (WebException ex) 72 { 73 //这样可以看见返回的错误详细,如果上传项目别这样写直接输出错误就行 74 LogHelper.WriteErrorLog(ex); 75 var httpErrResponse = ((HttpWebResponse)ex.Response); 76 using (var stream = httpErrResponse.GetResponseStream()) 77 { 78 if (stream != null) 79 { 80 using (var reader = new StreamReader(stream)) 81 { 82 result = reader.ReadToEnd(); 83 } 84 } 85 } 86 } 87 return HttpUtility.HtmlDecode(result);//返回调用接口返回值 88 }
PS:HttpWebReq类详细:https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest(v=vs.110).aspx
二.调用方法
1 public string GetUserBalance(MemberInfo model)//这个参数需要什么就可以传什么 2 { 3 LogHelper.WriteLog(string.Format("TingZi_Unique:{0}", DateTime.Now));//写日志 4 string PostUrl = "接口地址"; 5 string JsonStr = "{\"username\":\"" + model.UserName + "\"}";//JSON类型参数,参数写这里 6 string retData = string.Empty; 7 retData = UtilsHelper.HttpWebRequest(PostUrl, JsonStr, Encoding.UTF8, contentType: "application/json", method: "POST");//调用方法 8 //现在获取到了retData想判断什么判断什么 9 return "你想返回什么就返回什么!"; 10 }
PS:小菜一只请多多指教