POST-HTTPS authentication error

This is a simple entry to the https site using the C # console application, I used the same with webservice. When I ran this, it froze. The violinist is downloaded and on the Auth tab I see No proxy authentication header. No WWW-Authenticate header.

I used to use Stream instead of MemoryStream. I commented on some of the things that I used before, but did not work as a preauthenticate.

I can log in to get a subscriber via IE using the same user and password. Can someone please tell me what happened?

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


        }
    }
}
+3
source share
4 answers

byteData MemoryStream? HttpWebRequest.GetRequestStream, , , .

+1

- , sooooo, :

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, , , , .

+1

, , , . , , . , .

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

maybe try setting credentials in the request header. I was working on a project where I need to request some data from a web application using spring security, and the only way to get it working is to set credentials in the request header.

the code looks something like this:

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

See this blog post for more details.

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

0
source

Source: https://habr.com/ru/post/1779020/