方式1:基于QT的QNetworkReply访问http
#include <QtNetwork>
#include <QtCore>
class RequestManager: public QObject
{
Q_OBJECT
public:
explicit RequestManager(QObject *parent = nullptr, QString header = QString("text/xml"));
bool request(QUrl m_url);
QString getResponse() { return m_response; }
signals:
void finished();
private slots:
void requestFinished();
void requestReadyRead();
private:
bool isHttpRedirect() const;
void reportRedirect();
QUrl m_url;
QNetworkAccessManager m_manager;
QNetworkReply *m_currentDownload = nullptr;
QString m_response;
QString m_header;
};
#include "requestmanager.h"
#include <QDebug>
#include <cstdio>
using namespace std;
RequestManager::RequestManager(QObject *parent, QString header)
: QObject(parent)
{
m_response = "";
m_header = header;
}
bool RequestManager::request(QUrl aUrl)
{
m_url = aUrl;
QNetworkRequest request(m_url);
request.setRawHeader("Accept", m_header.toUtf8());
m_currentDownload = m_manager.get(request);
connect(m_currentDownload, SIGNAL(readyRead()),
SLOT(requestReadyRead()));
connect(m_currentDownload, SIGNAL(finished()),
SLOT(requestFinished()));
return true;
}
void RequestManager::requestFinished()
{
if (m_currentDownload->error()) {
qWarning() << "Request Error: " << m_currentDownload->errorString();
} else {
if (isHttpRedirect()) {
reportRedirect();
m_response = "";
QUrl lastUrl = m_currentDownload->request().url();
m_currentDownload->deleteLater();
if (lastUrl != m_url)
request(m_url);
return;
}
}
QList<QNetworkReply::RawHeaderPair> l = m_currentDownload->rawHeaderPairs();
int sz = l.count();
for(int i = 0; i<sz; i++)
{
QNetworkReply::RawHeaderPair pair = l[i];
if (pair.first == "Content-Type")
{
QString responseHeader = pair.second;
if (!((m_header.contains("json", Qt::CaseInsensitive) && responseHeader.contains("json", Qt::CaseInsensitive)) ||
(m_header.contains("xml", Qt::CaseInsensitive) && responseHeader.contains("xml", Qt::CaseInsensitive))))
m_response = "MT Agent does not support request protocol " + m_header;
break;
}
}
m_currentDownload->deleteLater();
emit finished();
}
void RequestManager::requestReadyRead()
{
m_response += m_currentDownload->readAll();
}
bool RequestManager::isHttpRedirect() const
{
int statusCode = m_currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
return statusCode == 301 || statusCode == 302 || statusCode == 303
|| statusCode == 305 || statusCode == 307 || statusCode == 308;
}
void RequestManager::reportRedirect()
{
int statusCode = m_currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QUrl requestUrl = m_currentDownload->request().url();
qWarning() << "Request: " << requestUrl.toDisplayString()
<< " was redirected with code: " << statusCode;
QVariant target = m_currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (!target.isValid())
return;
QUrl redirectUrl = target.toUrl();
if (redirectUrl.isRelative())
redirectUrl = requestUrl.resolved(redirectUrl);
qWarning() << "Redirected to: " << redirectUrl.toDisplayString();
m_url = redirectUrl;
}
错误:Request Error: “Connection closed”
方式2:基于c#访问 错误:Request Error: “Connection closed” 代码如下
String URL = "http://192.168.16.230:7878/probe";
HttpWebRequest myrq = (HttpWebRequest)WebRequest.Create(URL);
myrq.KeepAlive = true ;
myrq.Timeout = 30 * 1000;
myrq.Method = "Get";
myrq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8";
myrq.Host = "192.168.16.230:7878";
myrq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36";
HttpWebResponse myrp = (HttpWebResponse)myrq.GetResponse();
if (myrp.StatusCode != HttpStatusCode.OK)
{
return;
}
using (StreamReader sr = new StreamReader(myrp.GetResponseStream()))
{
Console.Write(sr.ReadToEnd());
}
Console.ReadKey();
方式3:基于curl访问
curl http:
错误:curl: (1) Received HTTP/0.9 when not allowed
原因:因为此http server是HTTP/0.9版本,QT和C#都不支持这么低版本。 解决:curl --http0.9 http://192.168.16.230:7878 成功读取。
|