1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>HTTP Client</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="3"><!-- Empty --></A>
<H2>3 HTTP Client</H2>
<A NAME="3.1"><!-- Empty --></A>
<H3>3.1 Introduction</H3>
<P>The HTTP client will be started when the inets application is
started and is then available to all processes on that erlang
node. The client will spawn a new process to handle each request
unless there is a possibility to pipeline a request. The client
will add a host header and an empty te header if there are no such
headers present in the request. The client supports
ipv6 as long as the underlying mechanisms also do so.<A NAME="3.2"><!-- Empty --></A>
<H3>3.2 Configuration</H3>
<P>It is possible to configure what directory the HTTP client
should use to store information. Currently the only information
stored here is cookies. If the HTTP client service is not
configured all cookies will be treated as session cookies. Here
follows a description of a configuration entry for the HTTP client
in the application configuration file.
<PRE>
[{inets, [{services, [{httpc, {Profile, Dir}}]}]}]
</PRE>
<P>Profile = atom() - default is the only valid value, as
profiles are currently not supported.
<P> Dir = string()
<A NAME="3.3"><!-- Empty --></A>
<H3>3.3 Using the HTTP Client API</H3>
<PRE>
1 > application:start(inets).
ok
</PRE>
<P> Use the proxy "www-proxy.mycompany.com:8000",
but not for requsts to localhost. This will apply to all subsequent
requests
<PRE>
2 > http:set_options([{proxy, {{"www-proxy.mycompany.com", 8000},
["localhost"]}}]).
ok
</PRE>
<P> An ordinary synchronous request.
<PRE>
3 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
http:request(get, {"http://www.erlang.org", []}, [], []).
</PRE>
<P> With all default values, as above, a get request can also be written
like this.
<PRE>
4 > {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
http:request("http://www.erlang.org").
</PRE>
<P> An ordinary asynchronous request. The result will be sent
to the calling process on the form {http, {ReqestId, Result}}
<PRE>
5 > {ok, RequestId} =
http:request(get, {"http://www.erlang.org", []}, [], [{sync, false}]).
</PRE>
<P>In this case the calling process is the shell, so we receive the
result.
<PRE>
6 > receive {http, {RequestId, Result}} -> ok after 500 -> error end.
ok
</PRE>
<P> Send a request with a specified connection header.
<PRE>
7 > {ok, {{NewVersion, 200, NewReasonPhrase}, NewHeaders, NewBody}} =
http:request(get, {"http://www.erlang.org", [{"connection", "close"}]},
[], []).
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|