103 lines
3.5 KiB
C#
103 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Collections.Specialized;
|
|
using System.Reflection;
|
|
|
|
namespace HxBusiness
|
|
{
|
|
public class ConnectedManager
|
|
{
|
|
public bool isOpen(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return false;
|
|
}
|
|
Ping p = new Ping();
|
|
Uri uri = new Uri(url);
|
|
if (p.Send(uri.Host, 150).Status == IPStatus.Success)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 获取配置信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetUrl(string url)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return "-1";
|
|
}
|
|
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
|
request.Method = "GET";
|
|
request.ContentType = "application/json";
|
|
HttpWebResponse Response2 = request.GetResponse() as HttpWebResponse;
|
|
StreamReader reader = new StreamReader(Response2.GetResponseStream(), Encoding.UTF8);
|
|
string jsontmp = reader.ReadToEnd();
|
|
reader.Close();
|
|
Response2.Close();
|
|
return jsontmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
return "-2";
|
|
}
|
|
}
|
|
public string PostForUrl(string url, string postdb)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return "-1";
|
|
}
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(postdb);
|
|
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
|
//设置请求头
|
|
SetHeaderValue(request.Headers, "owl-ois-adapter-tag", "HUAXIAO");
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json";
|
|
request.ContentLength = byteArray.Length;
|
|
Stream newStream = request.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
|
|
newStream.Write(byteArray, 0, byteArray.Length);
|
|
newStream.Close();
|
|
HttpWebResponse Response2 = request.GetResponse() as HttpWebResponse;
|
|
StreamReader reader = new StreamReader(Response2.GetResponseStream(), Encoding.UTF8);
|
|
string jsontmp = reader.ReadToEnd();
|
|
reader.Close();
|
|
Response2.Close();
|
|
return jsontmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("服务错误:" + ex.ToString() + "提示:" + ex.StackTrace);
|
|
return "-2";
|
|
}
|
|
}
|
|
|
|
public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
|
|
{
|
|
var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
if (property != null)
|
|
{
|
|
var collection = property.GetValue(header, null) as NameValueCollection;
|
|
collection[name] = value;
|
|
}
|
|
}
|
|
}
|
|
}
|