你好,我是编程新手,所以我的问题可能有点奇怪。我的老板让我创建一个http post请求,使用一个密钥和一条消息来访问我们的客户机。
我已经看过Handle HTTP request in C# Console application这篇文章,但是它不包括我把密钥和消息放在哪里,这样客户端api就知道它是我了。提前感谢你的帮助。
最佳答案
我相信你想要这个:
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create("http://domain.com/page.aspx");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data,0,data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();