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.
Earlier I used Stream instead of MemoryStream. I've commented out some of the things I used before but didn't work like preauthenticate.
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 = "[email protected]";
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? You need to use HttpWebRequest.GetRequestStream to get the stream, write your post data, and make sure it is closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With