CSDN话题挑战赛第2期
参赛话题:Qt应用程序开发
QNetworkRequest类持有一个要用QNetworkAccessManager发送的请求。 QNetworkRequest是网络访问API的一部分,是持有通过网络发送请求所需信息的类。它包含一个URL和一些辅助信息,可以用来修改请求。 也请参见QNetworkReply和QNetworkAccessManager。
enum QNetworkRequest::Attribute
用于QNetworkRequest和QNetworkReply的属性代码。 属性是额外的元数据,用于控制请求的行为,并从回复中传递进一步的信息给应用程序。属性也是可扩展的,允许自定义实现传递自定义值。 下表解释了默认的属性代码是什么,与之相关的QVariant类型,如果所述属性缺失的默认值,以及它是在请求还是回复中使用。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::HttpStatusCodeAttribute | 0 | Replies only, type: QMetaType::Int (no default) Indicates the HTTP status code received from the HTTP server (like 200, 304, 404, 401, etc.). If the connection was not HTTP-based, this attribute will not be present. |
| QNetworkRequest::HttpReasonPhraseAttribute | 1 | Replies only, type: QMetaType::QByteArray (no default) Indicates the HTTP reason phrase as received from the HTTP server (like "Ok", "Found", "Not Found", "Access Denied", etc.) This is the human-readable representation of the status code (see above). If the connection was not HTTP-based, this attribute will not be present. |
| QNetworkRequest::RedirectionTargetAttribute | 2 | Replies only, type: QMetaType::QUrl (no default) If present, it indicates that the server is redirecting the request to a different URL. The Network Access API does not by default follow redirections: the application can determine if the requested redirection should be allowed, according to its security policies, or it can set QNetworkRequest::FollowRedirectsAttribute to true (in which case the redirection will be followed and this attribute will not be present in the reply). The returned URL might be relative. Use QUrl::resolved() to create an absolute URL out of it. |
| QNetworkRequest::ConnectionEncryptedAttribute | 3 | Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained through an encrypted (secure) connection. |
| QNetworkRequest::CacheLoadControlAttribute | 4 | Requests only, type: QMetaType::Int (default: QNetworkRequest::PreferNetwork) Controls how the cache should be accessed. The possible values are those of QNetworkRequest::CacheLoadControl. Note that the default QNetworkAccessManager implementation does not support caching. However, this attribute may be used by certain backends to modify their requests (for example, for caching proxies). |
| QNetworkRequest::CacheSaveControlAttribute | 5 | Requests only, type: QMetaType::Bool (default: true) Controls if the data obtained should be saved to cache for future uses. If the value is false, the data obtained will not be automatically cached. If true, data may be cached, provided it is cacheable (what is cacheable depends on the protocol being used). |
| QNetworkRequest::SourceIsFromCacheAttribute | 6 | Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained from cache or not. |
| QNetworkRequest::DoNotBufferUploadDataAttribute | 7 | Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to buffer the upload data, e.g. when doing a HTTP POST. When using this flag with sequential upload data, the ContentLengthHeader header must be set. |
| QNetworkRequest::HttpPipeliningAllowedAttribute | 8 | Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP pipelining with this request. |
| QNetworkRequest::HttpPipeliningWasUsedAttribute | 9 | Replies only, type: QMetaType::Bool Indicates whether the HTTP pipelining was used for receiving this reply. |
| QNetworkRequest::CustomVerbAttribute | 10 | Requests only, type: QMetaType::QByteArray Holds the value for the custom HTTP verb to send (destined for usage of other verbs than GET, POST, PUT and DELETE). This verb is set when calling QNetworkAccessManager::sendCustomRequest(). |
| QNetworkRequest::CookieLoadControlAttribute | 11 | Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to send 'Cookie' headers in the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
| QNetworkRequest::CookieSaveControlAttribute | 13 | Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to save 'Cookie' headers received from the server in reply to the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
| QNetworkRequest::AuthenticationReuseAttribute | 12 | Requests only, type: QMetaType::Int (default: QNetworkRequest::Automatic) Indicates whether to use cached authorization credentials in the request, if available. If this is set to QNetworkRequest::Manual and the authentication mechanism is 'Basic' or 'Digest', Qt will not send an an 'Authorization' HTTP header with any cached credentials it may have for the request's URL. This attribute is set to QNetworkRequest::Manual by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
| QNetworkRequest::BackgroundRequestAttribute | 17 | Type: QMetaType::Bool (default: false) Indicates that this is a background transfer, rather than a user initiated transfer. Depending on the platform, background transfers may be subject to different policies. The QNetworkSession ConnectInBackground property will be set according to this attribute. |
| QNetworkRequest::SpdyAllowedAttribute | 18 | Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use SPDY with this request. This applies only to SSL requests, and depends on the server supporting SPDY. |
| QNetworkRequest::SpdyWasUsedAttribute | 19 | Replies only, type: QMetaType::Bool Indicates whether SPDY was used for receiving this reply. |
| QNetworkRequest::HTTP2AllowedAttribute | 22 | Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP/2 with this request. This applies to SSL requests or 'cleartext' HTTP/2. |
| QNetworkRequest::HTTP2WasUsedAttribute | 23 | Replies only, type: QMetaType::Bool (default: false) Indicates whether HTTP/2 was used for receiving this reply. (This value was introduced in 5.9.) |
| QNetworkRequest::EmitAllUploadProgressSignalsAttribute | 20 | Requests only, type: QMetaType::Bool (default: false) Indicates whether all upload signals should be emitted. By default, the uploadProgress signal is emitted only in 100 millisecond intervals. (This value was introduced in 5.5.) |
| QNetworkRequest::FollowRedirectsAttribute | 21 | Requests only, type: QMetaType::Bool (default: false) Indicates whether the Network Access API should automatically follow a HTTP redirect response or not. Currently redirects that are insecure, that is redirecting from "https" to "http" protocol, are not allowed. (This value was introduced in 5.6.) |
| QNetworkRequest::OriginalContentLengthAttribute | 24 | Replies only, type QMetaType::Int Holds the original content-length attribute before being invalidated and removed from the header when the data is compressed and the request was marked to be decompressed automatically. (This value was introduced in 5.9.) |
| QNetworkRequest::RedirectPolicyAttribute | 25 | Requests only, type: QMetaType::Int, should be one of the QNetworkRequest::RedirectPolicy values (default: ManualRedirectPolicy). This attribute obsoletes FollowRedirectsAttribute. (This value was introduced in 5.9.) |
| QNetworkRequest::User | 1000 | Special type. Additional information can be passed in QVariants with types ranging from User to UserMax. The default implementation of Network Access will ignore any request attributes in this range and it will not produce any attributes in this range in replies. The range is reserved for extensions of QNetworkAccessManager. |
| QNetworkRequest::UserMax | 32767 | Special type. See User. |
enum QNetworkRequest::CacheLoadControl
控制QNetworkAccessManager的缓存机制。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::AlwaysNetwork | 0 | always load from network and do not check if the cache has a valid entry (similar to the "Reload" feature in browsers); in addition, force intermediate caches to re-validate. |
| QNetworkRequest::PreferNetwork | 1 | default value; load from the network if the cached entry is older than the network entry. This will never return stale data from the cache, but revalidate resources that have become stale. |
| QNetworkRequest::PreferCache | 2 | load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache. |
| QNetworkRequest::AlwaysCache | 3 | only load from cache, indicating error if the item was not cached (i.e., off-line mode) |
enum QNetworkRequest::KnownHeaders
QNetworkRequest所解析的已知头类型的列表。每个已知的头也以原始形式表示,并附有完整的HTTP名称。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::ContentDispositionHeader | 6 | Corresponds to the HTTP Content-Disposition header and contains a string containing the disposition type (for instance, attachment) and a parameter (for instance, filename). |
| QNetworkRequest::ContentTypeHeader | 0 | Corresponds to the HTTP Content-Type header and contains a string containing the media (MIME) type and any auxiliary data (for instance, charset). |
| QNetworkRequest::ContentLengthHeader | 1 | Corresponds to the HTTP Content-Length header and contains the length in bytes of the data transmitted. |
| QNetworkRequest::LocationHeader | 2 | Corresponds to the HTTP Location header and contains a URL representing the actual location of the data, including the destination URL in case of redirections. |
| QNetworkRequest::LastModifiedHeader | 3 | Corresponds to the HTTP Last-Modified header and contains a QDateTime representing the last modification date of the contents. |
| QNetworkRequest::CookieHeader | 4 | Corresponds to the HTTP Cookie header and contains a QList<QNetworkCookie> representing the cookies to be sent back to the server. |
| QNetworkRequest::SetCookieHeader | 5 | Corresponds to the HTTP Set-Cookie header and contains a QList<QNetworkCookie> representing the cookies sent by the server to be stored locally. |
| QNetworkRequest::UserAgentHeader | 7 | The User-Agent header sent by HTTP clients. |
| QNetworkRequest::ServerHeader | 8 | The Server header received by HTTP clients. |
参见 header(), setHeader(), rawHeader(), 和setRawHeader().
enum QNetworkRequest::LoadControl
表示请求的加载机制的某个方面是否被手动重写,例如被Qt WebKit重写。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::Automatic | 0 | default value: indicates default behaviour. |
| QNetworkRequest::Manual | 1 | indicates behaviour has been manually overridden. |
enum QNetworkRequest::Priority
该枚举列出了可能的网络请求优先级。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::HighPriority | 1 | High priority |
| QNetworkRequest::NormalPriority | 3 | Normal priority |
| QNetworkRequest::LowPriority | 5 | Low priority |
enum QNetworkRequest::RedirectPolicy
表示网络访问API是否应该自动跟踪HTTP重定向响应。
| Constant | Value | Description |
|---|---|---|
| QNetworkRequest::ManualRedirectPolicy | 0 | Default value: not following any redirects. |
| QNetworkRequest::NoLessSafeRedirectPolicy | 1 | Only "http"->"http", "http" -> "https" or "https" -> "https" redirects are allowed. Equivalent to setting the old FollowRedirectsAttribute to true |
| QNetworkRequest::SameOriginRedirectPolicy | 2 | Require the same protocol, host and port. Note, Example Domain and Example Domain will fail with this policy (implicit/explicit ports are considered to be a mismatch). |
| QNetworkRequest::UserVerifiedRedirectPolicy | 3 | Client decides whether to follow each redirect by handling the redirected() signal, emitting redirectAllowed() on the QNetworkReply object to allow the redirect or aborting/finishing it to reject the redirect. This can be used, for example, to ask the user whether to accept the redirect, or to decide based on some app-specific configuration. |
QNetworkRequest::QNetworkRequest(const QUrl &url = QUrl())
构造一个QNetworkRequest对象,将url作为被请求的URL。 参见url()和setUrl()。
QNetworkRequest::QNetworkRequest(const QNetworkRequest &other)
创建一个other的副本。
QNetworkRequest::~QNetworkRequest()
析构函数
QVariant QNetworkRequest::attribute(Attribute code, const QVariant &defaultValue = QVariant()) const
返回与代码编码相关的属性。如果该属性没有被设置,它将返回defaultValue。 注意:这个函数不应用QNetworkRequest::Attribute中列出的默认值。 另请参见setAttribute()和QNetworkRequest::Attribute。
bool QNetworkRequest::hasRawHeader(const QByteArray &headerName) const
如果这个网络请求中存在原始头信息headerName,则返回true。 参见rawHeader()和setRawHeader()。
QVariant QNetworkRequest::header(KnownHeaders header) const
如果此请求中存在已知的网络头文件,则返回该文件的值。如果它不存在,则返回QVariant()(即,一个无效的变量)。 参见KnownHeaders, rawHeader(), and setHeader()。
int QNetworkRequest::maximumRedirectsAllowed() const
返回这个请求允许遵循的最大重定向数量。 这个函数在Qt 5.6中引入。 也请参见setMaximumRedirectsAllowed()。
QObject *QNetworkRequest::originatingObject() const
返回对发起这个网络请求的对象的引用;如果没有设置或该对象已被销毁,则返回0。 这个函数在Qt 4.6中引入。 也请参见setOriginatingObject()。
Priority QNetworkRequest::priority() const
返回这个请求的优先级。 这个函数在Qt 4.7中被引入。 也见setPriority()。
QByteArray QNetworkRequest::rawHeader(const QByteArray &headerName) const
返回头信息headerName的原始形式。如果没有这样的头,将返回一个空的QByteArray,这可能与一个存在但没有内容的头无法区分(使用hasRawHeader()来找出头是否存在)。 原始头可以用setRawHeader()或setHeader()来设置。 参见header()和setRawHeader()。
QList<QByteArray> QNetworkRequest::rawHeaderList() const
返回该网络请求中设置的所有原始头信息的列表。该列表是按照头信息被设置的顺序排列的。 参见hasRawHeader()和rawHeader()。
void QNetworkRequest::setAttribute(Attribute code, const QVariant &value)
将与code代码相关的属性设置为value值。如果该属性已经被设置,那么之前的值将被丢弃。在特殊情况下,如果value是一个无效的QVariant,该属性将被取消设置。 参见 attribute() 和 QNetworkRequest::Attribute。
void QNetworkRequest::setHeader(KnownHeaders header, const QVariant &value)
将已知头信息的值设置为value,覆盖之前设置的任何头信息。这个操作也会设置相应的原始HTTP头。 参见KnownHeaders, setRawHeader(), and header().
void QNetworkRequest::setMaximumRedirectsAllowed(int maxRedirectsAllowed)
将此请求允许遵循的最大重定向数量设置为maxRedirectsAllowed。 这个函数在Qt 5.6中引入。 也请参见maximumRedirectsAllowed()。
void QNetworkRequest::setOriginatingObject(QObject object*)**
允许设置一个对发起请求的对象的引用。 例如,Qt WebKit将始发对象设置为发起请求的QWebFrame。 这个函数在Qt 4.6中被引入。 参见originatingObject()。
void QNetworkRequest::setPriority(Priority priority)
将此请求的优先级设置为优先级。 注意:优先级只是对网络访问管理器的一个提示。它可以使用它,也可以不使用。目前它被用于HTTP,以决定哪个请求应该被首先发送到服务器上。 这个函数是在Qt 4.7中引入的。 也请看优先级()。
void QNetworkRequest::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
将头信息headerName设置为headerValue的值。如果headerName对应于一个已知的头(见QNetworkRequest::KnownHeaders),原始格式将被解析,相应的 "熟 "头也将被设置。 比如说:
request.setRawHeader(QByteArray("Last-Modified"), QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"));
也会将已知的头LastModifiedHeader设置为解析后的日期的QDateTime对象。 注意:两次设置相同的头信息会覆盖之前的设置。为了完成多个同名HTTP头的行为,你应该把两个值连接起来,用逗号(",")隔开,然后设置一个单一的原始头信息。 参见KnownHeaders, setHeader(), hasRawHeader(), and rawHeader()。
void QNetworkRequest::setSslConfiguration(const QSslConfiguration &config)
设置此网络请求的SSL配置为config。适用的设置是私钥、本地证书、SSL协议(SSLv2、SSLv3、TLSv1.0,如适用)、CA证书和SSL后端允许使用的密码。 默认情况下,没有设置SSL配置,这使得后端可以自由选择最适合他们的配置。 参见sslConfiguration()和QSslConfiguration::defaultConfiguration()。
void QNetworkRequest::setUrl(const QUrl &url)
将此网络请求所指的URL设置为url。 也请参见url()。
QSslConfiguration QNetworkRequest::sslConfiguration() const
返回这个网络请求的SSL配置。默认情况下,没有指定SSL设置。 参见setSslConfiguration()。
void QNetworkRequest::swap(QNetworkRequest &other)
将此网络请求与其他网络请求交换。这个函数非常快,而且从不失败。 这个函数在Qt 5.0中引入。
QUrl QNetworkRequest::url() const
返回这个网络请求所指向的URL。 参见setUrl()。
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
网络编程套接字网络编程基础知识理解源`IP`地址和目的`IP`地址理解源MAC地址和目的MAC地址认识端口号理解端口号和进程ID理解源端口号和目的端口号认识`TCP`协议认识`UDP`协议网络字节序socket编程接口`sockaddr``UDP`网络程序服务器端代码逻辑:需要用到的接口服务器端代码`udp`客户端代码逻辑`udp`客户端代码`TCP`网络程序服务器代码逻辑多个版本服务器单进程版本多进程版本多线程版本线程池版本服务器端代码客户端代码逻辑客户端代码TCP协议通讯流程TCP协议的客户端/服务器程序流程三次握手(建立连接)数据传输四次挥手(断开连接)TCP和UDP对比网络编程基础知识
在前面两节的例子中,主界面窗口的尺寸和标签控件显示的矩形区域等,都是用C++代码编写的。窗口和控件的尺寸都是预估的,控件如果多起来,那就不好估计每个控件合适的位置和大小了。用C++代码编写图形界面的问题就是不直观,因此Qt项目开发了专门的可视化图形界面编辑器——QtDesigner(Qt设计师)。通过QtDesigner就可以很方便地创建图形界面文件*.ui,然后将ui文件应用到源代码里面,做到“所见即所得”,大大方便了图形界面的设计。本节就演示一下QtDesigner的简单使用,学习拖拽控件和设置控件属性,并将ui文件应用到Qt程序代码里。使用QtDesigner设计界面在开始菜单中找到「Q
我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)
在我的路线文件中我有:match'graphs/(:id(/:action))'=>'graphs#(:action)'如果是GET请求(工作)或POST请求(不工作),我想匹配它我知道我可以使用以下方法在资源中声明POST请求:post'/'=>:show,:on=>:member但是我怎样才能为比赛做到这一点呢?谢谢。 最佳答案 如果你同时想要POST和GETmatch'graphs/(:id(/:action))'=>'graphs#(:action)',:via=>[:get,:post]编辑默认值可以设置如下match'g
是否可以在不实际下载文件的情况下检查文件是否存在?我有这么大的(~40mb)文件,例如:http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm这与ruby不严格相关,但如果发件人可以设置内容长度就好了。RestClient.get"http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm",headers:{"Content-Length"=>100} 最佳答案
我在这方面尝试了很多URL,在我遇到这个特定的之前,它们似乎都很好:require'rubygems'require'nokogiri'require'open-uri'doc=Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))putsdoc这是结果:/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in`open_http':404NotFound(OpenURI::HT
我试图像这样在我的测试用例中执行获取:request.env['CONTENT_TYPE']='application/json'get:index,:application_name=>"Heka"虽然,它失败了:ActionView::MissingTemplate:Missingtemplatealarm_events/indexwith{:handlers=>[:builder,:haml,:erb,:rjs,:rhtml,:rxml],:locale=>[:en,:en],:formats=>[:html]尽管在我的Controller中我有:respond_to:html,