我们在请求接口的时候,有时因为跨域的问题,总是请求接口失败,亦或是请求接口时,页面还存在跳转的问题,这个时候,我们通过前台ajax请求自己的一般处理程序,用一般处理程序请求客户提供的接口
//获取token
public string gettoken(HttpContext context)
{
string strResult = "";
try
{
string code = context.Request.QueryString["code"];
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("userName" + ":" + "pwd")); //验证用户的身份,否则会报401(未授权)的错误,
LogHelper.WriteLog(code);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://vcc.hbasesoft.com:8162/oauth/token?client_id=69D8274D631711E880ADDF64DF6ADFSD&code=" + code + "&grant_type=authorization_code&scope=read write&redirect_uri=http://218.194.184.150:5901/versioninfo.html");
myRequest.Method = "POST";
myRequest.ContentType = "text/xml";
myRequest.Headers.Add("Authorization: " + " Basic " + user);//添加验证头
try
{
HttpWebResponse HttpWResp = (HttpWebResponse)myRequest.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.UTF8);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
}
strResult = strBuilder.ToString();
}
catch (Exception exp)
{
strResult = "error:" + exp.Message;
LogHelper.WriteLog(strResult);
}
}
catch (Exception exp)
{
strResult = "error:" + exp.Message;
LogHelper.WriteLog(strResult);
}
return strResult;
}