相关文章推荐
淡定的盒饭  ·  ASP.NET 核心 Blazor ...·  1 月前    · 
傻傻的馒头  ·  STRING_SPLIT ...·  1 月前    · 
犯傻的黄豆  ·  Branches API | GitLab ...·  3 周前    · 
踢足球的泡面  ·  如何利用 GPT-4 ...·  2 年前    · 
豪情万千的皮带  ·  MongoDB ...·  2 年前    · 
没有腹肌的豆腐  ·  USE WMI and WMQ to ...·  2 年前    · 
string getGscUserUrl = " http:/xxx.com/GscHandler.ashx " ;
// 加入参数,用于更新请求
string urlHandler = getGscUserUrl + " ?id= " + Guid.NewGuid();
webRequest = (HttpWebRequest)HttpWebRequest.Create(urlHandler);
webRequest.Timeout = 3000 ; // 3秒超时
// 调用ashx,并取值
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string currentUserGulid = responseReader.ReadToEnd();
return currentUserGulid.Trim();
catch
return "" ;
finally
responseReader.Close();
responseReader.Dispose();

需要授权时写法如下:

public string GetGscCurrentUser()
{
HttpWebRequest webRequest = null ;
StreamReader responseReader = null ;
try
{
string getGscUserUrl = System.Configuration.ConfigurationManager.AppSettings[ " GscGetUserUrl " ];
string urlHandler = getGscUserUrl + " ?id= " + Guid.NewGuid();
webRequest = (HttpWebRequest)HttpWebRequest.Create(urlHandler);
webRequest.Timeout = 3000 ; // 3秒超时
webRequest.PreAuthenticate = true ;
NetworkCredential gscCred = new NetworkCredential( " account " , " *** " );
webRequest.Credentials = gscCred;

responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string currentUserGulid = responseReader.ReadToEnd();
return currentUserGulid.Trim();
}
catch
{
return "" ;
}
finally
{
responseReader.Close();
responseReader.Dispose();
}
}