我想使用我的 asp.net 应用程序进行 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";
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)未经授权”
提前致谢