POST 到 HTTPS 身份验证错误 [英] POST to HTTPS authentication error
问题描述
这是一个使用 C# 控制台应用程序发布到 https 站点的简单帖子,我也对 webservice 使用了相同的内容.当我运行它时它冻结了.下载了提琴手,在 Auth 选项卡中我看到不存在代理验证标头.不存在 WWW-Authenticate 标头.
This is a simple post to a https site using C# console app, I used the same thing with webservice too. When I run this it froze. Downloaded the fiddler and in the Auth tab I see No Proxy-Authenticate Header is present. No WWW-Authenticate Header is present.
之前我使用 Stream 而不是 MemoryStream.我已经注释掉了我之前使用的一些东西,但没有像 preauthenticate 那样工作.
Earlier I used Stream instead of MemoryStream. I've commented out some of the things I used before but didn't work like preauthenticate.
我可以使用相同的用户和密码登录该站点以通过 IE 获取订阅者.有人可以告诉我有什么问题吗?
I can login to the site to get a subscriber thru IE, using the same user and passsword. 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());
}
}
}
}
}
推荐答案
当您可以简单地编写如此多的代码时,您是否有任何特别的原因:
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));
}
这也将负责对您的请求参数进行正确的 url 编码,这样您就不必手动进行.
This will also take care of properly url encoding your request parameters so that you don't have to do it manually.
当我运行它时,我得到了 401 Unauthorized,但我猜这是因为用于基本身份验证的用户名和密码是虚拟的.
When I run this I got 401 Unauthorized but I guess that's because the username and password used for basic authentication are dummy.
这篇关于POST 到 HTTPS 身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!