public static bool channelExists(string url) { try { using (var client = new NewWebClient()) { client.HeadOnly = true; // fine, if channel exists // throws Exception on 404 client.DownloadString(url); } return(true); } catch (WebException) { return(false); } }
public static bool channelExists(string channel) { try { using (var client = new NewWebClient()) { client.HeadOnly = true; // fine, no content downloaded // throws 404 client.DownloadString("https://api.twitch.tv/kraken/channels/" + channel); } return(true); } catch { return(false); } }
public static bool channelExists(string channel) { try { using (var client = new NewWebClient()) { string s = client.DownloadString(String.Format(channelInfoUrl, channel)); if (s.Contains("mvc.js', null)")) { return(false); } } return(true); } catch { return(false); } }
public string getViewers() { JObject jObject; string info; try { using (NewWebClient cl = new NewWebClient()) { info = cl.DownloadString(String.Format(infoUrl, channelName)); } jObject = JsonConvert.DeserializeObject <JObject>(info); return(jObject["spectators"].ToString()); } catch (Exception ex) { Debug.WriteLine("Viewers exception: " + ex.Message); return("0"); } }
/// <summary> /// 向文件服务器上传文件 /// </summary> /// <param name="file">文件流</param> /// <param name="fileExtension">文件类型</param> /// <returns>返回文件服务地址,错误返回:Error</returns> private static async Task <string> UploadFileForByte(byte[] file, string fileExtension = "") { await Task.Delay(10); if (fileExtension.IsNullOrEmpty()) { fileExtension = file.GetExtension(); } NewWebClient myWebClient = new NewWebClient(); myWebClient.Timeout = 1000; //设置超时时间 myWebClient.Credentials = CredentialCache.DefaultCredentials; myWebClient.Headers.Add("TxoooUploadFileType", fileExtension); byte[] getArray = await myWebClient.UploadDataTaskAsync("http://file.txooo.cc/UpLoadForByte.ashx", file); string getstr = Encoding.Default.GetString(getArray); return(getstr.Replace("http:", "https:")); }
/// <summary> /// 将数据发送到业务系统中 /// </summary> /// <param name="url"></param> /// <param name="data"></param> public void PostMessage(string url, string data, ILogWriter _log) { //发送消息到回调接口 if (string.IsNullOrEmpty(url)) { _log.Info("CenterInterface配置为空"); return; } _log.Info($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")} 回调{url}接口。"); //实例化 NewWebClient client = new NewWebClient(); //参数转流 byte[] bytearray = Encoding.UTF8.GetBytes(data); //采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 client.Headers.Add("Content-Type", "application/json;charset=UTF-8"); client.Headers.Add("ContentLength", bytearray.Length.ToString());//长度 //上传,post方式,并接收返回数据(这是同步,需要等待接收返回值) try { byte[] responseData = client.UploadData(url, "POST", bytearray); //释放 client.Dispose(); //处理返回数据 string rel = Encoding.UTF8.GetString(responseData); if (!string.IsNullOrEmpty(rel)) { _log.Info($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff")} 回调{url}接口返回内容:{rel}"); } } catch (Exception ex) { _log.Info($"发送Post数据时出错:{ex.Message}"); } }