XMLをRESTサービスに送信しようとしています。私が使用しているコードは次のとおりです。XMLをRESTに送信中にエラーが発生しました
サービスの呼び出し中に次のエラーが発生しています。
リモートサーバーからエラーが返されました。(401)Unauthorized。
は、私はまた、あなたの助けに直接すなわち
NetworkCredential nc = new NetworkCredential(username, password);
serviceRequest.Credentials = nc;
感謝をNetworkCredentialsを設定しようとしています。
Uri address = new Uri("https://localhost:30000/restservice/");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/json";
string data = @"<Sample XML Here>";
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(address, "Basic", new NetworkCredential(username, password));
request.Credentials = mycache;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
Response.Write(reader.ReadToEnd());
}
Darrelに感謝します。私はフィドラーから何を得ているのですか:プロキシなし - 認証ヘッダーが存在します。 WWW認証ヘッダーは存在しません。 –
うわーそれは吸う。 HTTP仕様では、401には常にWWW-Authenticateヘッダーが付いている必要があります。あなたのサーバーの技術は何ですか? –
私はWindows Server 2008 R2で作業しています。ここで私が参照した記事があります。これは最終的にhttp://msdn.microsoft.com/en-us/library/debx8sh9.aspxで動作しました。上記のWebClientとサンプルを使用しても同じ問題があります。 –