如何创建HTTP POST请求 [英] How to create HTTP post request

查看:497
本文介绍了如何创建HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在编程新的,所以我的问题可能是有点奇怪。我的老板问我要创建使用键和访问我们的客户消息的HTTP POST请求。

Hello I'm new at programing so my question might be a little bit odd. My boss ask me to create a HTTP post request using a key and a message to access our client.

我已经看到了文章<一个href=\"http://stackoverflow.com/questions/16587847/handle-http-request-in-c-sharp-console-application\">Handle在C#控制台应用程序HTTP请求,但不包括在哪里我把钥匙和消息,以便客户端API知道它的我。鸭preciate的帮助下提前。

I already seen the article Handle HTTP request in C# Console application but it doesn't include where I put the key and message so the client API knows its me. Appreciate the help in advance.

推荐答案

我相信你想这样的:

    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();

这篇关于如何创建HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆