简体   繁体   English

POST到HTTPS身份验证错误

[英]POST to HTTPS authentication error

This is a simple post to a https site using C# console app, I used the same thing with webservice too. 这是一个使用C#控制台应用程序的https网站的简单帖子,我也使用了与webservice相同的东西。 When I run this it froze. 当我跑这个它冻结了。 Downloaded the fiddler and in the Auth tab I see No Proxy-Authenticate Header is present. 下载了提琴手,在Auth选项卡中,我看到No Proxy-Authenticate Header存在。 No WWW-Authenticate Header is present. 没有WWW-Authenticate标头。

Earlier I used Stream instead of MemoryStream. 之前我使用的是Stream而不是MemoryStream。 I've commented out some of the things I used before but didn't work like preauthenticate. 我已经注释掉了之前使用的一些东西,但是没有像preauthenticate那样工作。

I can login to the site to get a subscriber thru IE, using the same user and passsword. 我可以使用相同的用户和密码登录该站点以通过IE获取订阅者。 Can some one please tell me what's wrong?. 有人可以告诉我有什么不对吗?

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());
                }
            }


        }
    }
}

You are taking the data in byteData and writing it over itself via a MemoryStream? 您正在获取byteData中的数据并通过MemoryStream将其写入自身? You need to use HttpWebRequest.GetRequestStream to get the stream, write your post data, and make sure it is closed. 您需要使用HttpWebRequest.GetRequestStream来获取流,编写您的发布数据,并确保它已关闭。

Is there any particular reason you would like to write sooooo much code when you could simply: 有什么特别的原因,你可以简单地写下这么多代码:

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));
}

This will also take care of properly url encoding your request parameters so that you don't have to do it manually. 这还将处理正确的url编码您的请求参数,以便您不必手动执行。

When I run this I got 401 Unauthorized but I guess that's because the username and password used for basic authentication are dummy. 当我运行这个时,我得到了401 Unauthorized,但我想这是因为用于基本身份验证的用户名和密码是虚拟的。

maybe try to set credential in the request header. 也许尝试在请求标头中设置凭据。 I worked on a project where I have to request some data from a web application using spring security and the only way I can get it working is by set credential in the request header. 我参与了一个项目,我必须使用spring安全性从Web应用程序请求一些数据,我可以通过请求标头中的set credential来实现它的唯一工作方式。

the code looks something like this: 代码看起来像这样:

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

for more details see this blog post 有关详细信息,请参阅此博客文章

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

I copied your code and it wouldn't run because you were not doing anything with the posted data stream before getting the response. 我复制了你的代码并且它不会运行,因为你在获得响应之前没有对发布的数据流做任何事情。 I've copied the code and it works as expected now. 我已经复制了代码,它现在按预期工作。 Just subst your real credentials and it is good. 只是替换你的真实凭据,这是好的。

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM