C# geoserver中的身份验证-asp.net中的POST方法
我想用我的asp.net应用程序在geoserver中进行POST调用以进行身份验证,这是我的代码C# geoserver中的身份验证-asp.net中的POST方法,c#,asp.net,geoserver,C#,Asp.net,Geoserver,我想用我的asp.net应用程序在geoserver中进行POST调用以进行身份验证,这是我的代码 HttpWebRequest req = WebRequest.Create(new Uri("http://localhost:1979/geoserver/rest")) as HttpWebRequest; req.Method = "POST"; req.ContentType = "application/xml"; string authInfo = "admin:geoserver"
HttpWebRequest req = WebRequest.Create(new Uri("http://localhost:1979/geoserver/rest")) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/xml";
string authInfo = "admin:geoserver";
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic"+ authInfo));
// Build a string with all the params, properly encoded.
// We assume that the arrays paramName and paramVal are
// of equal length:
StringBuilder paramz = new StringBuilder();
for (int i = 0; i < paramName.Length; i++)
{
paramz.append(paramName[i]);
paramz.append("=");
paramz.append(HttpUtility.UrlEncode(paramVal[i]));
paramz.append("&");
}
paramz.Append("username");
paramz.Append("=");
paramz.Append("admin");
paramz.Append("&");
paramz.Append("password");
paramz.Append("=");
paramz.Append("geoserver");
CredentialCache cc = new CredentialCache();
cc.Add(
new Uri("http://localhost:1979/geoserver/rest"),
"Basic",
new NetworkCredential("admin", "geoserver", "localhost"));
req.Credentials = new NetworkCredential("admin", "geoserver", "localhost");
//req.Credentials = CredentialCache.DefaultCredentials;
//req.Credentials = cc;
//req.AllowAutoRedirect = true;
// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Pick up the response:
string result = null;
HttpWebResponse resp=null;
try
{
resp = (HttpWebResponse)req.GetResponse();
using (resp as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
}
catch (WebException we)
{
HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound)
{
}
}
我收到这个错误远程服务器返回了一个错误:401未经授权,即使我包括我的登录凭据
提前感谢更改此行
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic"+ authInfo));
到
授权头实际上是带有嵌入空间的基本YYXXZZETCEC
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic "+ authInfo));