此代码向服务器发送请求。我正在使用端点打开一个套接字。我对 POST 内容长度有疑问。我没有使用 HttpWebRequst 而是一个套接字(来自我们的客户端平台),它连接到他们的服务器平台。这是发送一个长字符串请求。
Header = POST /OKMMIS HTTP/5.1
Request = header + content
Content-length = content.length
Content = @"ISA*00* *00* *ZZ*500000355 *ZZ*734476619 *130711*1023*^*00501*790719345*0*T*:~GS*HS*500000355*734476619*20130711*1023*273450*X*005010X279A1~ST*270*0001*005010X279A1~BHT*0022*13*635091349826842989*20130711*1023~HL*1**20*1~NM1*PR*2*Oklahoma Medicaid*****PI*734476619~HL*2*1*21*1~NM1*1P*2*Hillcrest Medical Center*****XX*1629057229~HL*3*2*22*0~TRN*1*20130711102317CDN266905*9CARDONHCN~NM1*IL*1*MESSNDREW*MARQUIS****MI*B10083667~REF*SY*323006707~DMG*D8*20100108*M~DTP*291*D8*20130512~EQ*30~SE*14*0001~GE*1*273450~IEA*1*790719345~";
呼叫m_socClient.Send(requestBytes)
发送此消息。
他们的接收服务器日志给出了这个错误:
message[From HTTPUtil::getHttpRequest():Bytes to read is less than ZERO ???, req->headerLengh 为 49, req->contentLength 为 486,我们已经读取了 537 个字节
标头长度= 51。内容长度= 486。但是服务器读取539字节?我收到一个 HTTP 400 错误请求。这是来自服务器的响应
HTTP/1.1 400 Bad Request
Content-Length: 539
POST /OKMMISPOS HTTP/5.1
Content-Length: 486
ISA*00* *00* *ZZ*500000355 *ZZ*734476619 *130718*1129*^*00501*936078483*0*T*:~GS*HS*500000355*734476619*20130718*1129*326565*X*005010X279A1~ST*270*0001*005010X279A1~BHT*0022*13*635097437938418629*20130718*1129~HL*1**20*1~NM1*PR*2*Oklahoma Medicaid*****PI*734476619~HL*2*1*21*1~NM1*1P*2*Hillcrest Medical Center*****XX*1629057229~HL*3*2*22*0~NM1*IL*1*MARQUIS*****MI*B17993667~DMG*D8*20101208*M~DTP*291*D8*20130512~EQ*30~SE*12*0001~GE*1*326565~IEA*1*936078483~
我什至从哪里开始?
//// create a new client socket ...
string szIPSelected = "127.0.0.1";
int szPort = 50050;
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, szPort);
m_socClient.Connect(remoteEndPoint);
if (m_socClient.Connected)
{
toolStripStatusLabel1.Text = "Connected to " + szIPSelected + ":" + szPort;
statusStrip1.Refresh();
string resource = "/OKMMISPOS";
var header = String.Format("POST {0} HTTP/5.1 " + Environment.NewLine, resource);
String body = @"ISA*00* *00* *ZZ*500000355 *ZZ*734476619 *130711*1023*^*00501*790719345*0*T*:~GS*HS*500000355*734476619*20130711*1023*273450*X*005010X279A1~ST*270*0001*005010X279A1~BHT*0022*13*635091349826842989*20130711*1023~HL*1**20*1~NM1*PR*2*Oklahoma Medicaid*****PI*734476619~HL*2*1*21*1~NM1*1P*2*Hillcrest Medical Center*****XX*1629057229~HL*3*2*22*0~TRN*1*20130711102317CDN266905*9CARDONHCN~NM1*IL*1*MESSNDREW*MARQUIS****MI*B10083667~REF*SY*323006707~DMG*D8*20100108*M~DTP*291*D8*20130512~EQ*30~SE*14*0001~GE*1*273450~IEA*1*790719345~";
byte[] bodyBytes = Encoding.ASCII.GetBytes(body);
header += "Content-Length: " + (bodyBytes.Length) + Environment.NewLine + Environment.NewLine + Environment.NewLine;
string request = String.Concat(header, body);
textBox3.Text = request;
textBox3.Refresh();
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
m_socClient.Send(requestBytes);
toolStripStatusLabel1.Text = "Sending Request to " + szIPSelected + ":" + szPort;
statusStrip1.Refresh();
}
Thread.Sleep(3000);
byte[] bytes = new byte[1024];
int bytesRec = m_socClient.Receive(bytes);
txtResponse271.Text = Encoding.ASCII.GetString(bytes, 0, bytesRec);
m_socClient.Disconnect(true);
m_socClient.Close();