C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权相关的知识,希望对你有一定的参考价值。
需要将本系统页面去请求一个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();
* 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
参考技术C 要先登录,保持COOKIE再访问你上面的地址。追问
我试过先登录需验证的系统,然后再打开当前页面,但是还是不行提示401错误
追答COOKIE容器,要使用。保持前面的验证后COOKIE,再访问
以上是关于C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权的主要内容,如果未能解决你的问题,请参考以下文章
C# HttpWebRequest 绝技 根据URL地址获取网页信息
VS10中用C#写的HttpWebRequest请求的url被自动修改编码,无法获取想要的结果
在c#用httpwebrequest中发送get/http/https请求后,怎么获得数据