我要为一件(可能)非常愚蠢的事情发疯了…QNetworkReply finished信号是在服务器发送数据之后发出的。
我有一个非常简单的vb服务器Public Class WebServer
Public Shared Sub Main()
Try
Dim strIP As String = "serverip"
Dim hostName As String = Dns.GetHostName()
Dim serverIP As IPAddress = Nothing
Dim checkIP As IPAddress = Nothing
Dim strPort As String = "9071"
For Each checkIP In Dns.GetHostEntry(hostName).AddressList()
DebugLog("Checking IP: " & checkIP.ToString)
If checkIP.ToString = strIP Then
serverIP = checkIP
End If
Next
Dim tcpListener As New TcpListener(serverIP, Int32.Parse(strPort))
tcpListener.Start()
Dim clientSocket As System.Net.Sockets.Socket
clientSocket = tcpListener.AcceptSocket()
Dim respByte() As Byte = Encoding.ASCII.GetBytes("replystring")
clientSocket.Send(respByte, 0, respByte.Length, SocketFlags.None)
clientSocket.Shutdown(SocketShutdown.Both)
clientSocket.Close()
Catch ex As Exception
MsgBox("Exception")
End Try
End Sub
End Class
当我用qt调用一个空字符串返回…然后会产生一个错误"在接收和处理整个应答之前,远程服务器过早地关闭了连接"
下面的客户端请求代码
void TESTJson::makeTheCallBtnPressed()
{
ui.label->clear();
ui.label_2->clear();
QUrl url("serveraddressandport");
QString method = "methodname";
url.setPath(QString("%1%2").arg(url.path()).arg(method));
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QByteArray postData; // test
postData.append("param1=hi");
networkManager= new QNetworkAccessManager();
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_NETAM_finished(QNetworkReply*)));
reply = networkManager->post(request, postData);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(_downloadProgress(qint64, qint64)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(_error(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(_finished()));
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(_metaDataChanged()));
connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(_sslErrors(const QList<QSslError> &)));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(_uploadProgress(qint64, qint64)));
}
有人能帮我一下吗? 已解决…在发送数据之前,我们还需要发送报头以引发完成的信号
' Set HTML Header
Dim htmlHeader As String = _
"HTTP/1.0 200 OK" & ControlChars.CrLf & _
"Content-Length: " & respByte.Length & ControlChars.CrLf & _
"Content-Type: " & getContentType(strBuff) & _
ControlChars.CrLf & ControlChars.CrLf
' The content Length of HTML Header
Dim headerByte() As Byte = Encoding.ASCII.GetBytes(htmlHeader)
' Send HTML Header back to Web Browser
clientSocket.Send(headerByte, 0, headerByte.Length, SocketFlags.None)