Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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中HTTP Post请求并从控制台应用程序发送响应#_C#_Http_Httpwebrequest - Fatal编程技术网

C# 如何在C中HTTP Post请求并从控制台应用程序发送响应#

C# 如何在C中HTTP Post请求并从控制台应用程序发送响应#,c#,http,httpwebrequest,C#,Http,Httpwebrequest,我有一个客户端和服务器,如下所示。 在客户方面 HttpWebRequest toClient = (HttpWebRequest)WebRequest.Create("http://localhost:10000"); toClient.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; toClient.Accept = "application/json, tex

我有一个客户端和服务器,如下所示。 在客户方面

HttpWebRequest toClient = (HttpWebRequest)WebRequest.Create("http://localhost:10000");
            toClient.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            toClient.Accept = "application/json, text/javascript, */*; q=0.01";
            toClient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
            String DatatoClient = "ToAccountNumber=" + value.ToAccountNumber + "&Amount=" + value.Amount;
            toClient.Method = "POST";

            byte[] byteArray = Encoding.UTF8.GetBytes(DatatoClient);
            toClient.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = toClient.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);


            //String DatatoProxy = "Account=" + 232303 + "&Amount=" + 200;

            //ProxySocket.Send(ASCIIEncoding.ASCII.GetBytes(toClient.ToString()));

            HttpWebResponse HttpResp = (HttpWebResponse)toClient.GetResponse();
            Stream respStream = HttpResp.GetResponseStream();
            StreamReader readStream = new StreamReader(respStream, Encoding.UTF8);

            Console.WriteLine("Response stream received.");
            String receivedResp=readStream.ReadToEnd();
在服务器上(在我的例子中是一个控制台应用程序),我有

bool recvRequest=true;
字符串EOL=“\r\n”;
int bodylength=0;
字符串requestPayload=“”;
字符串requestTempLine=“”;
List requestLines=new List();
字节[]请求缓冲区=新字节[1];
字节[]响应缓冲=新字节[1];
requestLines.Clear();
尝试
{
//状态0:处理来自客户端的请求
while(recvRequest)
{
while(this.clientSocket.Available!=0)
{
this.clientSocket.Receive(requestBuffer);
string fromByte=ascienceoding.ASCII.GetString(requestBuffer);
requestPayload+=fromByte;
requestTempLine+=fromByte;
if(requestTempLine.EndsWith(EOL))
{
Add(requestTempLine.Trim());
requestTempLine=“”;
}
if(requestPayload.Contains(EOL+EOL))
{
string[]list=requestLines.ToArray();
字符串体=”;
foreach(列表中的字符串s)
{
如果(s.包含(“内容长度”))
{
bodylength=Convert.ToInt32(s.Split(“:”)[1]);
对于(int i=0;i
上面的代码是一个套接字处理程序。我不确定这是否是在控制台应用程序中处理Http请求的正确方法。我试图做的是从控制台应用程序将收到的Http请求作为Http消息发送一个响应,消息体为“是”或“否”。客户端将从该控制台应用程序接收响应,并按下a键相应地

我不知道如何将Http响应返回给客户端,有人能告诉我我是否做得对,或者这是一种更好的方式吗


谢谢,

您为什么要费心自己从头开始创建此服务器?您只需在控制台应用程序中托管一个WCF服务,公开一些与Http兼容的绑定。这看起来是一个更简单、更干净的解决方案

这里有一个来自MSDN的示例


你有关于如何做的资源吗?我是C#的新手。因此我想到了在控制台应用程序和套接字中处理这个问题。我刚刚添加了一个链接……你需要在服务器上创建一个RESTful Web服务。请看一看,让你开始吧。
bool recvRequest = true;
        string EOL = "\r\n";
        int bodylength = 0;
        string requestPayload = "";
        string requestTempLine = "";
        List<string> requestLines = new List<string>();
        byte[] requestBuffer = new byte[1];
        byte[] responseBuffer = new byte[1];
        requestLines.Clear();

        try
        {
            //State 0: Handle Request from Client
            while (recvRequest)
            {
                while (this.clientSocket.Available != 0)
                {
                    this.clientSocket.Receive(requestBuffer);
                    string fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
                    requestPayload += fromByte;
                    requestTempLine += fromByte;

                    if (requestTempLine.EndsWith(EOL))
                    {
                        requestLines.Add(requestTempLine.Trim());
                        requestTempLine = "";
                    }

                    if (requestPayload.Contains(EOL + EOL))
                    {
                        string[] list = requestLines.ToArray();
                        string body = "";
                        foreach (string s in list)
                        {
                            if (s.Contains("Content-Length"))
                            {
                                bodylength = Convert.ToInt32(s.Split(':')[1]);
                                for (int i = 0; i < bodylength; i++)
                                {
                                    this.clientSocket.Receive(requestBuffer);
                                    fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
                                    body += fromByte;
                                }
                                requestPayload += body;
                                requestLines.Add(body);

                                AccountNo = Convert.ToInt64(body.Split('&')[0].Split('=')[1]);
                                Amount = Convert.ToInt64(body.Split('&')[1].Split('=')[1]);
                            }
                        }
                        recvRequest = false;
                    }
                }
            }
            Console.WriteLine("Raw Request Received...as \n {0}",requestPayload);
            Console.WriteLine("Received the Account Number as = {0}", AccountNo);
            Console.WriteLine("Received the Amount as = {0}", Amount);
            Console.WriteLine("Server Received the above Details are they Correct ?\n Type 'Y' or 'Yes' for Yes and 'N' or 'No' for No.");