1

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";
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) Unauthorized」というエラーが表示されます

前もって感謝します

4

1 に答える 1

0

この行を変更

req.Headers["Authorization"] =  Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic"+ authInfo));

req.Headers["Authorization"] =  Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic "+ authInfo));

Authorization ヘッダーには、実際Basic YyXxZzEtcEtcにはスペースが埋め込まれています。

于 2012-07-25T16:12:39.080 に答える