public string GetConn(string companyCode)
{
// url地址格式:WebService地址+方法名称
// WebService地址:../serverdb/ServerDBService.asmx
// 方法名称: GetCon
//string url = "../serverdb/ServerDBService.asmx/GetCon";
string _url = AppSettingItems.Configuration["Setting:GetDBConnStrService:Url"];
string _username = AppSettingItems.Configuration["Setting:GetDBConnStrService:UserName"];
string _password = AppSettingItems.Configuration["Setting:GetDBConnStrService:Password"];
// 参数
Dictionary<string, string> dicParam = new Dictionary<string, string>();
dicParam.Add("username", _username);
dicParam.Add("password", _password);
dicParam.Add("companycode", companyCode);
// 将参数转化为HttpContent
HttpContent httpContent = new FormUrlEncodedContent(dicParam);
// contentType对应
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync(_url, httpContent).Result;
var statusCode = response.StatusCode.ToString();
var result = response.Content.ReadAsStringAsync().Result;
//LogHelper.Debug("GetConn result:" + result);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
string _connStr = string.Empty;
try
{
//处理XML
//var status = xmlDoc.DocumentElement["Status"].InnerXml;
}
catch (Exception e)
{
throw new Exception($"{e.Message}!");
}
return _connStr;
}
【C#通过HttpClient 访问WEbService】
最新推荐文章于 2024-08-03 23:17:32 发布