【发布时间】: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