单步跟踪可以接收数据,直接运行就不行

lhj0532 2006-03-15 02:54:24
Socket 发送Http Post请求WebService,返回数据,代码如下:
string strData = "strAssetCode=10221457";
Socket httpSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
httpSocket.Connect(new IPEndPoint(IPAddress.Parse("192.168.39.97"),8090));
string sendStr = "POST /TestService/TestService.asmx/GetAsset HTTP/1.1\r\n";
sendStr = sendStr + "Host: 192.168.39.97\r\n";
sendStr = sendStr + "Content-Type: application/x-www-form-urlencoded\r\n";
sendStr = sendStr + "Content-Length: " + strData.Length + "\r\n\r\n";
sendStr = sendStr + strData;
byte[] sendBuff = Encoding.UTF8.GetBytes(sendStr);
httpSocket.Send(sendBuff);
byte[] recvBuff = new byte[1024 * 10];
int iRet = httpSocket.Receive(recvBuff);//单步跟踪这里就可以正确接收数据
string recvStr = Encoding.UTF8.GetString(recvBuff,0,iRet);


StreamWriter mWriter = new StreamWriter(Application.StartupPath + "\\Debug.txt");
//mWriter.WriteLine(sendStr);
mWriter.WriteLine(iRet);
mWriter.WriteLine(recvStr);
mWriter.Close();

httpSocket.Shutdown(SocketShutdown.Both);
httpSocket.Close();
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhj0532 2006-03-15
  • 打赏
  • 举报
回复
问题自己解决,直接运行结果被看成一个包,而后续数据还没有到达,就Receive返回。单步执行有时间延迟,所以后续数据也就发送到达,结果数据就正确!
感谢楼上的参与!
iseelxj 2006-03-15
  • 打赏
  • 举报
回复
我也遇到过类似的问题。顶顶
lhj0532 2006-03-15
  • 打赏
  • 举报
回复
直接运行结果:
112
HTTP/1.1 100 Continue
Server: Microsoft-IIS/5.0
Date: Wed, 15 Mar 2006 06:51:22 GMT
X-Powered-By: ASP.NET


lhj0532 2006-03-15
  • 打赏
  • 举报
回复
单步跟踪结果:
611
HTTP/1.1 100 Continue
Server: Microsoft-IIS/5.0
Date: Wed, 15 Mar 2006 06:50:42 GMT
X-Powered-By: ASP.NET

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 15 Mar 2006 06:50:42 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 270

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Schmidt">
<string>10221457 </string>
<string>1-PDH框供电模块</string>
</ArrayOfString>

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧