Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用C语言发送带有表单数据的帖子#_C# - Fatal编程技术网

C# 如何用C语言发送带有表单数据的帖子#

C# 如何用C语言发送带有表单数据的帖子#,c#,C#,我正试图做一个程序,要求我的网站与用户名,密码,硬件ID和一个职位的关键 我这里有一段代码,它应该向我的网站发送一个包含表单数据的POST请求,但是当它发送时,我的Web服务器会报告它没有收到POST数据 try { string poststring = String.Format("username={0}&password={1}&key={2}&hwid={3}", Username, P

我正试图做一个程序,要求我的网站与用户名,密码,硬件ID和一个职位的关键

我这里有一段代码,它应该向我的网站发送一个包含表单数据的POST请求,但是当它发送时,我的Web服务器会报告它没有收到POST数据

try
            {
                string poststring = String.Format("username={0}&password={1}&key={2}&hwid={3}", Username, Password, "272453745345934756392485764589", GetHardwareID());
                HttpWebRequest httpRequest =
    (HttpWebRequest)WebRequest.Create("mywebsite");

                httpRequest.Method = "POST";
                httpRequest.ContentType = "application/x-www-form-urlencoded";

                byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
                httpRequest.ContentLength = bytedata.Length;

                Stream requestStream = httpRequest.GetRequestStream();
                requestStream.Write(bytedata, 0, bytedata.Length);
                requestStream.Close();


                HttpWebResponse httpWebResponse =
                (HttpWebResponse)httpRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();

                StringBuilder sb = new StringBuilder();

                using (StreamReader reader =
                new StreamReader(responseStream, System.Text.Encoding.UTF8))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        sb.Append(line);
                    }
                }

                return sb.ToString();
            }
            catch (Exception Error)
            {
                return Error.ToString();
            }
如果有人能帮助我,我将不胜感激。

如文件所示

我们不建议您在新开发中使用
HttpWebRequest
。相反,使用类

HttpClient
只包含异步API,因为Web请求需要等待。在等待响应时冻结整个应用程序是不好的

因此,这里有一些异步函数,可以使用
HttpClient
发出
POST
请求,并向那里发送一些数据

首先,分别创建
HttpClient

HttpClient
旨在为每个应用程序而不是每次使用实例化一次

private static readonly HttpClient=new HttpClient();
然后实现该方法

专用异步任务PostHTTPRequestAsync(字符串url,字典数据)
{
使用(HttpContent-formContent=new-FormUrlEncodedContent(数据))
{
使用(HttpResponseMessage response=await client.PostAsync(url,formContent.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
返回wait response.Content.ReadAsStringAsync().configurewait(false);
}
}
}
或C#8.0

专用异步任务PostHTTPRequestAsync(字符串url,字典数据)
{
使用HttpContent formContent=newformurlencodedcontent(数据);
使用HttpResponseMessage response=wait client.PostAsync(url,formContent.ConfigureWait(false));
response.EnsureSuccessStatusCode();
返回wait response.Content.ReadAsStringAsync().configurewait(false);
}
看起来比你的代码简单,对吗

调用方异步方法如下所示

private async Task MyMethodAsync()
{
    Dictionary<string, string> postData = new Dictionary<string, string>();
    postData.Add("message", "Hello World!");
    try
    {
        string result = await PostHTTPRequestAsync("http://example.org", postData);
        Console.WriteLine(result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
专用异步任务MyMethodAsync()
{
Dictionary postData=新字典();
添加(“消息”,“你好,世界!”);
尝试
{
字符串结果=等待PostHTTPRequestAsync(“http://example.org“,postData);
控制台写入线(结果);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}

如果您不熟悉
async/await
,.

长寿命的HttpClients会带来自己的问题,@TomW,但这是另一个又长又有趣的故事。对于教育目的和小型应用程序,
HttpClient
的单个实例是一个很好的解决方案。这是否回答了您的问题?答案有用吗?