Sunday

csharp post form data

     
public string POST(string url, string username,string password, string postParams)
{


//Our getVars, to test the get of our php.
//We can get a page without any of these vars too though.
//string getVars = "?var1=test1&var2=test2";
//Initialization, we use localhost, change if applicable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.UserAgent = GetRndUserAgent();

string Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));

WebReq.Method = "POST";
WebReq.ContentType = "application/xml";
WebReq.AllowWriteStreamBuffering = true;

WebReq.Headers.Add("Authorization", "Basic " + Credentials);

WebReq.ServicePoint.Expect100Continue = false;



// string postParams = "name=some sort of value";
if (postParams != string.Empty)
{
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(postParams);
WebReq.ContentLength = buffer.Length;
using (Stream writer = WebReq.GetRequestStream())
{

writer.Write(buffer, 0, buffer.Length);

}
}

//From here on, it's all the same as above.
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
//_log(WebResp.StatusCode.ToString());
//_log(WebResp.Server);

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
return _Answer.ReadToEnd();


}

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...