【问题标题】:POST to HTTPS authentication errorPOST 到 HTTPS 身份验证错误
【发布时间】:2010-12-07 18:07:12
【问题描述】:

这是一个使用 C# 控制台应用程序发布到 https 站点的简单帖子,我在 web 服务中也使用了同样的东西。当我运行它时,它冻结了。下载了提琴手并在我看到的身份验证选项卡中 不存在 Proxy-Authenticate 标头。 不存在 WWW-Authenticate 标头。

之前我使用 Stream 而不是 MemoryStream。我已经注释掉了一些我以前使用过的东西,但没有像 preauthenticate 那样工作。

我可以使用相同的用户名和密码通过 IE 登录该站点以获取订阅者。 谁能告诉我怎么了?

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main()
        {
            Uri requestUri = new Uri("https://services.yesmail.com/enterprise/subscribers");            

            // Set the Method property of the request to POST.   
            CredentialCache cache = new CredentialCache();
            NetworkCredential nc = new NetworkCredential("user/user1", "password");                 
            cache.Add(requestUri, "Basic", nc);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);

            //request.PreAuthenticate = true;
            //request.KeepAlive = false;

            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/xml;charset=ISO-8859-1";

            //request.ContentType = "application/xml-www-form-urlencoded";       
            //request.Timeout = 300000;


            string EmailAddress = "test999@test1.com";
            string FirstName = "first";
            string LastName = "Last";

            StringBuilder Efulfill = new StringBuilder();


            Efulfill.Append("EmailAddress" + HttpUtility.UrlEncode(EmailAddress));
            Efulfill.Append("FirstName" + HttpUtility.UrlEncode(FirstName));
            Efulfill.Append("LastName" + HttpUtility.UrlEncode(LastName));


            byte[] byteData = Encoding.UTF8.GetBytes(Efulfill.ToString());

            request.ContentType = "application/xml;charset=ISO-8859-1";

            request.ContentLength = byteData.Length;



            using (MemoryStream Stream = new MemoryStream(byteData))
            {
                // Write the stream.
                Stream.Write(byteData, 0, byteData.Length);
                Stream.Close();
            }
            //Get response   

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream resStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(resStream, Encoding.Default);
                    Console.WriteLine(reader.ReadToEnd());
                }
            }


        }
    }
}

【问题讨论】:

  • 我仍然得到正确的凭据远程服务器返回错误:(401)未经授权,有什么想法吗?
  • 你有想过这个吗?

标签: c# web-services


【解决方案1】:

您正在获取 byteData 中的数据并通过 MemoryStream 将其写入自身?您需要使用HttpWebRequest.GetRequestStream 来获取流,写入您的帖子数据,并确保它已关闭。

【讨论】:

    【解决方案2】:

    你有什么特别的原因想写这么多代码吗?

    using (var client = new WebClient())
    {
        var requestUri = new Uri("https://services.yesmail.com/enterprise/subscribers");
        var cache = new CredentialCache();
        var nc = new NetworkCredential("user/user1", "password");
        cache.Add(requestUri, "Basic", nc);
        client.Credentials = cache;
    
        var values = new NameValueCollection
        {
            { "EmailAddress", "test999@test1.com" },
            { "FirstName", "first" },
            { "LastName", "last" },
        };
    
        var result = client.UploadValues(requestUri, values);
        Console.WriteLine(Encoding.Default.GetString(result));
    }
    

    这还将负责正确地对您的请求参数进行 url 编码,这样您就不必手动进行。

    当我运行这个时,我得到了 401 Unauthorized 但我猜这是因为用于基本身份验证的用户名和密码是虚拟的。

    【讨论】:

    • 我现在收到此错误。远程服务器返回错误:(415) Unsupported Media Type.
    • 还添加了这个内容类型还是一样的错误415 request.ContentType = "application/xml;charset=ISO-8859-1";
    【解决方案3】:

    我复制了您的代码,但它无法运行,因为您在收到响应之前没有对发布的数据流执行任何操作。我已经复制了代码,它现在按预期工作。只需替换您的真实凭据即可。

    Uri requestUri = new Uri("https://services.yesmail.com/enterprise/subscribers");            
    
    // Set the Method property of the request to POST.   
    var cache = new System.Net.CredentialCache();
    var nc = new System.Net.NetworkCredential("user/user1", "password");                 
    cache.Add(requestUri, "Basic", nc);
    var request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(requestUri);
    
    //request.PreAuthenticate = true;
    //request.KeepAlive = false;
    
    request.Method = System.Net.WebRequestMethods.Http.Post;
    request.ContentType = "application/xml;charset=ISO-8859-1";
    
    request.ContentType = "application/xml-www-form-urlencoded";       
    //request.Timeout = 300000;
    
    string EmailAddress = "test999@test1.com";
    string FirstName = "first";
    string LastName = "Last";
    
    StringBuilder Efulfill = new StringBuilder();
    
    Efulfill.Append("EmailAddress" + System.Web.HttpUtility.UrlEncode(EmailAddress));
    Efulfill.Append("FirstName" + System.Web.HttpUtility.UrlEncode(FirstName));
    Efulfill.Append("LastName" + System.Web.HttpUtility.UrlEncode(LastName));
    
    byte[] byteData = Encoding.UTF8.GetBytes(Efulfill.ToString());
    
    request.ContentType = "application/xml;charset=ISO-8859-1";
    request.ContentLength = byteData.Length;
    
    Stream postStream = request.GetRequestStream();
    postStream.Write(byteData, 0, byteData.Length);
    postStream.Close();
    
    //Get response   
    
    using (var response = (System.Net.HttpWebResponse)request.GetResponse())
    {
        using (Stream resStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(resStream, Encoding.Default);
            Console.WriteLine(reader.ReadToEnd());
        }
    }
    

    【讨论】:

    • Still I get The remote server returned an error: (401) Unauthorized.
    • 我没有要测试的帐户。但是您应该能够将您的真实凭据放在那里并获得不同的东西。
    【解决方案4】:

    也许尝试在请求标头中设置凭据。我在一个项目中工作,我必须使用 Spring Security 从 Web 应用程序请求一些数据,我可以让它工作的唯一方法是在请求标头中设置凭据。

    代码如下所示:

    string authInfo = username + ":" + password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    request.Headers["Authorization"] = "Basic " + authInfo;
    

    有关更多详细信息,请参阅此博客文章

    http://charlie.cu.cc/2012/05/how-use-basic-http-authentication-c-web-request/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多