Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

HTTP メッセージの送受信

この例では、リダイレクトを伴い、ダイジェスト認証を求める可能性のあるサーバーに要求を送信する方法を説明します。

関数sendRequestは、最初の要求で自動的にリダイレクトと認証を行います。以降の要求では、リダイレクトの負担を担うのは望ましくありません。そのために、sendRequestは以前の要求で受信したクッキーを保存し、以降の要求で再利用します。

sendRequestでは、履歴の使用、既定のタイムアウトを変更するカスタムHTTPOptionsオブジェクトの再使用、および保存された資格情報の使用が示されます。

この例では、こうした結果を得るための強力な汎用メカニズムは示されません。具体的には、sendRequestは以降のすべてのメッセージでホストから受け取ったすべてのクッキーを、ドメイン、パス、有効期限、その他のプロパティに関係なく、そのホストに返します。また、sendRequestでは、資格情報やクッキーが MATLAB®セッションにまたがって保存されることはありません。それでも、sendRequestは多くの用途に十分適しています。

次のコードから関数sendRequestを作成します。

functionresponse = sendRequest(uri,request)% uri: matlab.net.URI% request: matlab.net.http.RequestMessage%的回应:matlab.net.http.ResponseMessage% matlab.net.http.HTTPOptions persists across requests to reuse previous% Credentials in it for subsequent authenticationspersistentoptions% infos is a containers.Map object where:% key is uri.Host;% value is "info" struct containing:% cookies: vector of matlab.net.http.Cookie or empty% uri: target matlab.net.URI if redirect, or emptypersistentinfosifisempty(options) options = matlab.net.http.HTTPOptions('ConnectTimeout',20);endifisempty(infos) infos = containers.Map;endhost = string(uri.Host);% get Host from URItry% get info struct for host in mapinfo = infos(host);if~isempty(info.uri)% If it has a uri field, it means a redirect previously% took place, so replace requested URI with redirect URI.uri = info.uri;endif~isempty(info.cookies)% If it has cookies, it means we previously received cookies from this host.%添加饼干头字段帐目ining all of them.request = request.addFields(matlab.net.http.field.CookieField(info.cookies));endcatch% no previous redirect or cookies for this hostinfo = [];end% Send request and get response and history of transaction.[response, ~, history] = request.send(uri, options);ifresponse.StatusCode ~= matlab.net.http.StatusCode.OKreturnend% Get the Set-Cookie header fields from response message in% each history record and save them in the map.arrayfun(@addCookies, history)% If the last URI in the history is different from the URI sent in the original% request, then this was a redirect. Save the new target URI in the host info struct.targetURI = history(end).URI;if~isequal(targetURI, uri)ifisempty(info)% no previous info for this host in map, create new oneinfos(char(host)) = struct('cookies',[],'uri',targetURI);else% change URI in info for this host and put it back in mapinfo.uri = targetURI; infos(char(host)) = info;endendfunctionaddCookies(记录)% Add cookies in Response message in history record% to the map entry for the host to which the request was directed.%ahost = record.URI.Host;%的host the request was sent tocookieFields = record.Response.getFields('Set-Cookie');ifisempty(cookieFields)returnendcookieData = cookieFields.convert();% get array of Set-Cookie structscookies = [cookieData.Cookie];% get array of Cookies from all structstry% If info for this host was already in the map, add its cookies to it.ainfo = infos(ahost); ainfo.cookies = [ainfo.cookies cookies]; infos(char(ahost)) = ainfo;catch% Not yet in map, so add new info struct.infos(char(ahost)) = struct('cookies',cookies,'uri',[]);endendend

関数を呼び出します。

request = matlab.net.http.RequestMessage; uri = matlab.net.URI('//www.tatmou.com/products'); response = sendRequest(uri,request)
response = ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×11 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0

実際の応答値は異なる場合があります。

参考

||||||