J'ai essayé :

# encoding : utf-8

require 'base64'
require 'rubygems'
require 'httparty'
require 'rubygems'
require 'net/https'
require 'uri'
require 'json'

class LbTimestamp

  # SOURCE : https://www.universign.eu/fr/web-services

=begin

  static InputStream doTsp(String login, String pwd, String hash, String
algo)
          throws Exception
        {
          URLConnection conn = new URL("https://ws.universign.eu/tsa/post/
").openConnection();
          conn.setDoOutput(true);
          conn.setDoInput(true);
          String authString = login + ":" + pwd;
          String authStringEnc = Base64.encode(authString);
          conn.setRequestProperty("Authorization", "Basic " +
authStringEnc);

          OutputStream out = conn.getOutputStream();
          String params = "hashAlgo=" + URLEncoder.encode(algo, "UTF-8") +
"&hashValue="
              + URLEncoder.encode(hash, "UTF-8") + "&withCert=" +
URLEncoder.encode("false", "UTF-8");
          out.write(params.getBytes("UTF-8"));
          out.flush();

          return conn.getInputStream();
        }
=end
  include HTTParty
  #hash = "test pour universign"
  def self.doTsp(login="toto",
pwd="titi",hash="cb493b4f9c0bda24f0443a11dad75b3a04aec8b78440c193eea4857e4d6f8725",algo="SHA256")
    authString = login + ":" + pwd
    authStringEnc = Base64.encode64(authString)
    url = URI.parse("https://ws.universign.eu/tsa/post/";)
    headers = {"Authorization" => "Basic " + authStringEnc}
    params = {:hashAlgo => CGI::escape(algo), :hashValue =>
CGI::escape(hash), :withCert => CGI::escape("false") }
    http = Net::HTTP.new(url.host, url.port)
    resp, data = http.post(url.path, params.to_json, headers)
    puts("resp = "+resp.to_s)
    puts("data = "+data.to_s)

    #result = JSON.parse(data)

  end

end

Et j'obtiens :
irb(main):011:0> LbTimestamp.doTsp
EOFError: end of file reached
        from /usr/lib/ruby/1.9.1/net/protocol.rb:135:in `read_nonblock'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
        from /usr/lib/ruby/1.9.1/net/http.rb:2211:in `read_status_line'
        from /usr/lib/ruby/1.9.1/net/http.rb:2200:in `read_new'
        from /usr/lib/ruby/1.9.1/net/http.rb:1183:in `transport_request'
        from /usr/lib/ruby/1.9.1/net/http.rb:1169:in `request'
        from /usr/lib/ruby/1.9.1/net/http.rb:1162:in `block in request'
        from /usr/lib/ruby/1.9.1/net/http.rb:627:in `start'
        from /usr/lib/ruby/1.9.1/net/http.rb:1160:in `request'
        from /usr/lib/ruby/1.9.1/net/http.rb:970:in `post'
        from /srv/lb-06/lb2_srv_web/lib/timestamp/lb_timestamp.rb:45:in
`doTsp'
        from (irb):11
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in
`start'
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in
`start'
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top
(required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'irb(main):012:0> load
"#{Rails.root}/lib/timestamp/lb_timestamp.rb"
=> nil
irb(main):013:0> LbTimestamp.doTsp
Errno::ECONNRESET: Connection reset by peer
        from /usr/lib/ruby/1.9.1/net/protocol.rb:135:in `read_nonblock'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
        from /usr/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
        from /usr/lib/ruby/1.9.1/net/http.rb:2211:in `read_status_line'
        from /usr/lib/ruby/1.9.1/net/http.rb:2200:in `read_new'
        from /usr/lib/ruby/1.9.1/net/http.rb:1183:in `transport_request'
        from /usr/lib/ruby/1.9.1/net/http.rb:1169:in `request'
        from /usr/lib/ruby/1.9.1/net/http.rb:1162:in `block in request'
        from /usr/lib/ruby/1.9.1/net/http.rb:627:in `start'
        from /usr/lib/ruby/1.9.1/net/http.rb:1160:in `request'
        from /usr/lib/ruby/1.9.1/net/http.rb:970:in `post'
        from /srv/lb-06/lb2_srv_web/lib/timestamp/lb_timestamp.rb:45:in
`doTsp'
        from (irb):13
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in
`start'
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in
`start'
        from
/var/lib/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top
(required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

Je reviens à la version précédente


Le 21 novembre 2011 18:18, Frédérick Sauvage
<[email protected]>a écrit :

> Raté,
> après un nouveau essai, je reçois :
> irb(main):001:0>  Timestamp.doTsp('[email protected]','PASSWORDOK')
> => #<HTTParty::Response:0x308d3a8 @parsed_response="@INVALID_REQUEST",
> @response=#<Net::HTTPBadRequest 400 J readbody=true>,
> @headers={"cache-control"=>["no-cache"], "pragma"=>["no-cache"],
> "content-type"=>["application/octet-stream"],
> "transfer-encoding"=>["chunked"]}>
> Qui se traduit par :
>
> *Lorsqu'une requête échoue, Universign indique un code d'erreur qui vous
> permet de comprendre et de résoudre le problème rencontré.*
>
>    - *400 - requête malformée ou option non supportée.*
>
>
>
> Le 21 novembre 2011 18:08, Frédérick Sauvage <[email protected]>a 
> écrit :
>
> En tâtonnant, j'arrive à :
>>
>> # encoding : utf-8
>>
>> require 'base64'
>> require 'rubygems'
>> require 'httparty'
>>
>> class Timestamp
>>
>>   # SOURCE : https://www.universign.eu/fr/web-services
>>
>> =begin
>>
>>
>>   static InputStream doTsp(String login, String pwd, String hash, String
>> algo)
>>           throws Exception
>>         {
>>           URLConnection conn = new URL("
>> https://ws.universign.eu/tsa/post/";).openConnection();
>>
>>           conn.setDoOutput(true);
>>           conn.setDoInput(true);
>>           String authString = login + ":" + pwd;
>>           String authStringEnc = Base64.encode(authString);
>>           conn.setRequestProperty("Authorization", "Basic " +
>> authStringEnc);
>>
>>           OutputStream out = conn.getOutputStream();
>>           String params = "hashAlgo=" + URLEncoder.encode(algo, "UTF-8")
>> + "&hashValue="
>>               + URLEncoder.encode(hash, "UTF-8") + "&withCert=" +
>> URLEncoder.encode("false", "UTF-8");
>>           out.write(params.getBytes("UTF-8"));
>>           out.flush();
>>
>>           return conn.getInputStream();
>>         }
>> =end
>>   include HTTParty
>>   #hash = "test pour universign"
>>   def self.doTsp(login="toto",
>> pwd="titi",hash="cb493b4f9c0bda24f0443a11dad75b3a04aec8b78440c193eea4857e4d6f8725",algo="SHA256")
>>
>>     authString = login + ":" + pwd
>>     authStringEnc = Base64.encode64(authString)
>>     base_uri "https://ws.universign.eu/tsa/post/";
>>     headers "Authorization" => "Basic " + authStringEnc
>>     params = {"hashAlgo" => CGI::escape(algo), "hashValue" =>
>> CGI::escape(hash), "withCert" => CGI::escape("false") }
>>     post "/", :query => params , :body => headers
>>   end
>>
>> end
>>
>> Je pense être proche du but car j'obtiens :
>> => #<HTTParty::Response:0x8 @parsed_response=nil,
>> @response=#<Net::HTTPUnauthorized 401 J readbody=true>,
>> @headers={"cache-control"=>["no-cache"], "pragma"=>["no-cache"],
>> "content-type"=>["application/octet-stream"],
>> "transfer-encoding"=>["chunked"], "www-authenticate"=>["Basic
>> realm=\"Universign\""]}>
>> avec ce login et mot de passe.
>> Mais aucune réponse avec les bons login/mot de passe. Si tout va bien, il
>> ne me manque que l'équivalent de conn.getInputStream() que je n'ai pas
>> trouvé.
>>
>> Le 21 novembre 2011 17:31, Bruno Muscolino <[email protected]> a
>> écrit :
>>
>> J'utilise ce même code pour me connecter aux WS de ma banque.
>>>
>>> :)
>>>
>>> Have fun
>>> Bruno
>>>
>>> On Nov 21, 2011, at 11:29 AM, Frédérick Sauvage wrote:
>>>
>>> Oui je cherches à utiliser leur WebService ... je vais essayer de me
>>> débrouiller avec cela et la doc de httparty tant bien que mal
>>>
>>> Le 21 novembre 2011 17:10, Bruno Muscolino <[email protected]> a
>>> écrit :
>>>
>>>> En gros ce que tu cherches a faire c'est de te connecter sur leur page
>>>> et utiliser leur WebServices?
>>>>
>>>> En ruby: (voir doc do httparty pour determiner ou non si tu veux qqch
>>>> dans la hash query)
>>>>
>>>> require 'rubygems'
>>>> require 'httparty'
>>>>
>>>> class LoginSomeWhere
>>>>  include HTTParty
>>>>  base_uri "https://YOURURL.COM";
>>>>  headers "content-type" => "application/x-www-form-urlencoded"
>>>>
>>>>  def self.login username, password
>>>>    post "/", :query => {  }, :body => { :username => username,
>>>> :password => password }
>>>>  end
>>>> end
>>>>
>>>> puts LoginSomeWhere.login "username", "password"
>>>>
>>>> Ca
>>>>
>>>> On Nov 21, 2011, at 10:06 AM, Frédérick Sauvage wrote:
>>>>
>>>> > Bonjour,
>>>> >
>>>> > Cherchant à créer un sceau d'horodatage via Universign, je me retrouve
>>>> > face à 3 codes pour réaliser cela ... évidemment nulle trace de Ruby.
>>>> > Je cherche actuellement comment m'adapter au Ruby (en partant du Java
>>>> > que je maitrise mieux), mais je rencontre des difficultés.
>>>> >
>>>> > Une bonne âme pourrait-elle m'aider ?
>>>> >
>>>> > Voici les différents codes :
>>>> >
>>>> > En Python :
>>>> >
>>>> >      #!/usr/bin/python
>>>> >
>>>> >      import urllib;
>>>> >      import urllib2;
>>>> >      import hashlib;
>>>> >      import base64;
>>>> >
>>>> >      # first we construct the parameters for the request
>>>> >      data = {};
>>>> >      data['hashAlgo'] = "SHA256";
>>>> >      data['withCert'] = "true";
>>>> >      data['hashValue'] = hashlib.sha256(dataToTimestamp).hexdigest();
>>>> >      params = urllib.urlencode(data);
>>>> >
>>>> >      # basic HTTP authentication is needed to access this service
>>>> >      headers = {};
>>>> >      auth = base64.encodestring(username + ":" + password);
>>>> >      headers["Authorization"] = "Basic " + auth;
>>>> >
>>>> >      # then the request itself
>>>> >      request = urllib2.Request("https://ws.universign.eu/tsa/post/";,
>>>> > params, headers);
>>>> >
>>>> >      # all is ready, the request is made
>>>> >      response = urllib2.urlopen(request);
>>>> >      tsp = response.read();
>>>> >
>>>> >
>>>> >
>>>> > En PHP :
>>>> >
>>>> >      $hashedDataToTimestamp = hash('sha256', $dataToTimestamp);
>>>> >      $dataToSend = array ('hashAlgo' => 'SHA256', 'withCert' =>
>>>> > 'true', 'hashValue' => $hashedDataToTimestamp);
>>>> >      $dataQuery = http_build_query($dataToSend);
>>>> >      $context_options = array (
>>>> >              'http' => array (
>>>> >                  'method' => 'POST',
>>>> >                  'header'=> "Content-type: application/x-www-form-
>>>> > urlencoded\r\n"
>>>> >                   ."Content-Length: " . strlen($dataQuery) . "\r\n"
>>>> >                   ."Authorization: Basic ".base64_encode($login.':'.
>>>> > $password)."\r\n",
>>>> >                  'content' => $dataQuery
>>>> >                )
>>>> >            );
>>>> >
>>>> >      $context = stream_context_create($context_options);
>>>> >      $fp = fopen("https://ws.universign.eu/tsa/post/";, 'r', false,
>>>> > $context);
>>>> >      $tsp = stream_get_contents($fp);
>>>> >
>>>> >
>>>> >
>>>> > En Java :
>>>> >
>>>> >      static InputStream doTsp(String login, String pwd, String hash,
>>>> > String algo)
>>>> >        throws Exception
>>>> >      {
>>>> >        URLConnection conn = new URL("https://ws.universign.eu/tsa/
>>>> > post/").openConnection();
>>>> >        conn.setDoOutput(true);
>>>> >        conn.setDoInput(true);
>>>> >        String authString = login + ":" + pwd;
>>>> >        String authStringEnc = Base64.encode(authString);
>>>> >        conn.setRequestProperty("Authorization", "Basic " +
>>>> > authStringEnc);
>>>> >
>>>> >        OutputStream out = conn.getOutputStream();
>>>> >        String params = "hashAlgo=" + URLEncoder.encode(algo, "UTF-8")
>>>> > + "&hashValue="
>>>> >            + URLEncoder.encode(hash, "UTF-8") + "&withCert=" +
>>>> > URLEncoder.encode("false", "UTF-8");
>>>> >        out.write(params.getBytes("UTF-8"));
>>>> >        out.flush();
>>>> >
>>>> >        return conn.getInputStream();
>>>> >      }
>>>> >
>>>> >
>>>> > Merci d'avance,
>>>> >    Frédéric
>>>> >
>>>> > --
>>>> > Vous avez reçu ce message, car vous êtes abonné au groupe
>>>> "Railsfrance" de Google Groups.
>>>> > Pour transmettre des messages à ce groupe, envoyez un e-mail à
>>>> l'adresse [email protected]
>>>> > Pour résilier votre abonnement envoyez un e-mail à l'adresse
>>>> [email protected]
>>>>
>>>> --
>>>> Vous avez reçu ce message, car vous êtes abonné au groupe "Railsfrance"
>>>> de Google Groups.
>>>> Pour transmettre des messages à ce groupe, envoyez un e-mail à
>>>> l'adresse [email protected]
>>>> Pour résilier votre abonnement envoyez un e-mail à l'adresse
>>>> [email protected]
>>>
>>>
>>>
>>>
>>> --
>>> SAUVAGE Frédéric
>>>
>>>
>>> --
>>> Vous avez reçu ce message, car vous êtes abonné au groupe "Railsfrance"
>>> de Google Groups.
>>> Pour transmettre des messages à ce groupe, envoyez un e-mail à l'adresse
>>> [email protected]
>>> Pour résilier votre abonnement envoyez un e-mail à l'adresse
>>> [email protected]
>>>
>>>
>>>  --
>>> Vous avez reçu ce message, car vous êtes abonné au groupe "Railsfrance"
>>> de Google Groups.
>>> Pour transmettre des messages à ce groupe, envoyez un e-mail à l'adresse
>>> [email protected]
>>> Pour résilier votre abonnement envoyez un e-mail à l'adresse
>>> [email protected]
>>>
>>
>>
>>
>> --
>> SAUVAGE Frédéric
>>
>>
>
>
> --
> SAUVAGE Frédéric
>
>


-- 
SAUVAGE Frédéric

-- 
Vous avez reçu ce message, car vous êtes abonné au groupe "Railsfrance" de 
Google Groups.
Pour transmettre des messages à ce groupe, envoyez un e-mail à l'adresse 
[email protected]
Pour résilier votre abonnement envoyez un e-mail à l'adresse 
[email protected]

Répondre à