C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权 20
需要将本系统页面去请求一个URL看是否能返回一些信息来判断已有另一个系统的登录信息,实际上就是实现单点登录,但是后台调试出现401错误,这个url用浏览器是可以访问的,不...
需要将本系统页面去请求一个URL看是否能返回一些信息来判断已有另一个系统的登录信息,实际上就是实现单点登录,但是后台调试出现401错误,这个url用浏览器是可以访问的,不知道是什么问题,请高手帮忙~~代码如下~
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create("http://portal.boe.com.cn/boe-SSOAuthen/index.jsp?ltpatoken=LQyfa6TV9rJWZ+uHtLyUL1IBVwDCuv7wmO72FRuxGpoI1m9Px7w2oyOJlpbHm7pB0hq4SscXnTcNMIXciyBjNJF+zboqe+9Jxdwer9zz2R8g67vrLJjoUNZ38pPkD72cQ0vCwjJol30zhW2ajZBj+vf+Rsj9h5uMYmIxEeq5t0De4m3WUdLrDhnn8Aky4it2zDyistaOexfP2nkDPEJmgjpGdtN/Qh2gLDRf74/RZHBSrTXr/W4MCfS4F6SkXKkYifoxQngeC2fMoemXFA89QaDTdbzElbjH1AHJRpTfMTH3XyBAQOVX7cuXPz6U7/B2HymtNa2acYa5VpZLXaUmTb2Eoa787967");
webrequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream stream = webreponse.GetResponseStream();
byte[] rsByte = new Byte[webreponse.ContentLength]; //save data in the stream
try
{
stream.Read(rsByte, 0, (int)webreponse.ContentLength);
return System.Text.Encoding.UTF8.GetString(rsByte, 0, rsByte.Length).ToString();
}
catch (Exception exp)
{
return exp.ToString();
} 展开
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create("http://portal.boe.com.cn/boe-SSOAuthen/index.jsp?ltpatoken=LQyfa6TV9rJWZ+uHtLyUL1IBVwDCuv7wmO72FRuxGpoI1m9Px7w2oyOJlpbHm7pB0hq4SscXnTcNMIXciyBjNJF+zboqe+9Jxdwer9zz2R8g67vrLJjoUNZ38pPkD72cQ0vCwjJol30zhW2ajZBj+vf+Rsj9h5uMYmIxEeq5t0De4m3WUdLrDhnn8Aky4it2zDyistaOexfP2nkDPEJmgjpGdtN/Qh2gLDRf74/RZHBSrTXr/W4MCfS4F6SkXKkYifoxQngeC2fMoemXFA89QaDTdbzElbjH1AHJRpTfMTH3XyBAQOVX7cuXPz6U7/B2HymtNa2acYa5VpZLXaUmTb2Eoa787967");
webrequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream stream = webreponse.GetResponseStream();
byte[] rsByte = new Byte[webreponse.ContentLength]; //save data in the stream
try
{
stream.Read(rsByte, 0, (int)webreponse.ContentLength);
return System.Text.Encoding.UTF8.GetString(rsByte, 0, rsByte.Length).ToString();
}
catch (Exception exp)
{
return exp.ToString();
} 展开
展开全部
需要一个STATIC的全局授权回调函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
* Created by SharpDevelop.
* User: Austin
* Date: 2015/10/16
* Time: 10:52
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Net;
using System.Text;
namespace nezha_api_test
{
public class Hello
{
private static string username = "***********";
private static string password = "**************";
public static void Main(string[] args)
{
try {
string url = "**************";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Credentials = GetCredentialCache(url, username, password);
request.Headers.Add("Authorization", GetAuthorization(username, password));
request.Method = "GET";
HttpWebResponse myResponse = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
reader.Close();
myResponse.Close();
Console.WriteLine(s);
Console.Read();
} catch (WebException e) {
StreamReader reader = new StreamReader(e.Response.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
Console.WriteLine(s);
Console.Read();
}
}
#region # 生成 Http Basic 访问凭证 #
private static CredentialCache GetCredentialCache(string uri, string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
return credCache;
}
private static string GetAuthorization(string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
}
#endregion
}
}
* Created by SharpDevelop.
* User: Austin
* Date: 2015/10/16
* Time: 10:52
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Net;
using System.Text;
namespace nezha_api_test
{
public class Hello
{
private static string username = "***********";
private static string password = "**************";
public static void Main(string[] args)
{
try {
string url = "**************";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Credentials = GetCredentialCache(url, username, password);
request.Headers.Add("Authorization", GetAuthorization(username, password));
request.Method = "GET";
HttpWebResponse myResponse = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
reader.Close();
myResponse.Close();
Console.WriteLine(s);
Console.Read();
} catch (WebException e) {
StreamReader reader = new StreamReader(e.Response.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
Console.WriteLine(s);
Console.Read();
}
}
#region # 生成 Http Basic 访问凭证 #
private static CredentialCache GetCredentialCache(string uri, string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
return credCache;
}
private static string GetAuthorization(string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
}
#endregion
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-06-24 · 知道合伙人软件行家
关注
展开全部
要先登录,保持COOKIE再访问你上面的地址。
追问
我试过先登录需验证的系统,然后再打开当前页面,但是还是不行提示401错误
追答
COOKIE容器,要使用。保持前面的验证后COOKIE,再访问
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询