我有一个客户端和服务器如下。 在客户端,
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;
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.");
上面的代码是一个Socket Handler。我不确定如果这是在控制台应用程序中处理Http请求的正确方法。我想要做的是从控制台应用程序发送一个响应,将收到的Http请求作为http消息,正文为是或否。客户端将从此控制台应用程序接收响应并进行相应的响应。
我不确定如何将Http响应返回给客户端,如果我做得对,有人能指出我是否是更好的方法。
谢谢,
答案 0 :(得分:1)
为什么你会从头开始自己创建这个服务器呢?您可以在控制台应用程序中简单地托管一个WCF服务,该服务公开了一些与Http兼容的绑定。它看起来更简单,更清洁
以下是MSDN的一个例子......
http://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx