客户端通过http协议给服务器端发一个消息,在重启客户端后发送的第一条数据总是不对。
客户端程序如下:
private void ButtonCancel_Click(object sender, EventArgs e)
{
string URL = "http://192.168.2.33:8899";
string meth = "D:/cassion";
string pars = "{\"MacID\":\"" + MacID.Text + "\",\"Static\":\"" + Static.Text + "\"}";
int m = WebSvcCaller.QueryPostWebService(URL, meth, pars);
}
public static int QueryPostWebService(String URL, String MethodName, string Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
byte[] data = EncodePars(Pars);
WriteRequestData(request, data);
return ReadXmlResponse(request.GetResponse());
}
服务器端:利用socket监听Post消息
每次客户端重启后点击发送按钮,收到的总是:
"POST /D:/cassion HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: 192.168.2.33:8899\r\nContent-Length: 33\r\nExpect: 100-continue\r\nConnection: Keep-Alive\r\n\r\n
以后收到的消息是正确的,如下:
"POST /D:/cassion HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: 192.168.2.33:8899\r\nContent-Length: 33\r\nExpect: 100-continue\r\n\r\n{\"MacID\":\"08152654\",\"Static\":\"1\"}
比第一次收到的少了个Connection: Keep-Alive,取代的是要发送的消息
|