You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Ryan julius <rj...@yahoo.fr> on 2004/12/13 12:45:22 UTC
HttpClient, HttpURLConnection, server Authentication with encoding
Hello,
I have this String : sXmlMessage = "<mainMessage>messageBody</mainMessage>"
I would like to send sXmlMessage to a server named: webMServer whose address is http://pyasmp:5126.
webMServer use Basic authentication, the format of authentication(credentials!?!) is admin:servAdmin
credentials and sXmlMessage must be encoded using BASE64Encoder.
My question:
1.) How can I POST sXmlMessage to webMServer using HttpClient?
Note: I have unsuccessfully tried the BasicAuhentication example, I still have this message:
[WARN] HttpMethodBase - -No credentials available for the 'webMethods' authentication realm at pyasmp Method failed: HTTP/1.0 401 [ISS.0084.9004] Access Denied
2.) As U can see in the following piece of code, I have also tried HttpUrlConnection; but there is no link with the HttpClient. What might be wrong with this code?
Thanks.
The code:
=======
client.getState().setCredentials("null", sUrl, new UsernamePasswordCredentials(sServerAuthName, sServerAuthPassword));
HttpURLConnection httpUrlConnection = null;
try {
httpUrlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//Codage de la chaine de login et du fichier XML.
String authentication = (new sun.misc.BASE64Encoder()).encode( (sServerAuthName + ":"+ sServerAuthPassword).getBytes() );
httpUrlConnection.setRequestProperty("Authorization", "Basic "+ authentication );
String sXmlMessageCoded = "";
/*
try {
sXmlMessageCoded = URLEncoder.encode(sXmlMessage, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
*/
sXmlMessageCoded = URLEncoder.encode(sXmlMessage);
OutputStream out = null;
try {
out = httpUrlConnection.getOutputStream();
out.write(sXmlMessageCoded.getBytes());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
String sInLine = null;
String sInFullLine = null;
BufferedReader inReader;
try {
inReader =
new BufferedReader(
new InputStreamReader(httpUrlConnection.getInputStream()));
while ((sInLine = inReader.readLine()) != null) {
sInFullLine+=sInLine;
}
} catch (IOException e1) {
// TODO Bloc catch auto-généré
e1.printStackTrace();
}
System.out.println("Résultat de la requête http : "+ new String(sInFullLine));
//************************************************
//************************************************
HttpMethod method = null;
// Relance de la requête si nécessaire ...
DefaultMethodRetryHandler methodRetry = new DefaultMethodRetryHandler();
methodRetry.setRetryCount(3);
//En cas de succès d'envoi de requête, ne pas reessayer.
methodRetry.setRequestSentRetryEnabled(false);
// Create a method instance.
if(sRequestMethod.equalsIgnoreCase("get")){
method = new GetMethod(sUrl);
((GetMethod)method).setMethodRetryHandler(methodRetry);
}else{
//Target URL ...
method = new PostMethod(sUrl);
((PostMethod)method).setMethodRetryHandler(methodRetry);
((PostMethod)method).setRequestBody(sXmlMessageCoded);
}
// Tell the GET method to automatically handle authentication. The
// method will use any appropriate credentials to handle basic
// authentication requests. Setting this value to false will cause
// any request for authentication to return with a status of 401.
// It will then be up to the client to handle the authentication.
method.setDoAuthentication( true );
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
setOutput(OUT_RESPONSE, responseBody);
logger.info(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
---------------------------------
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails !
Créez votre Yahoo! Mail
Avec Yahoo! faites un don et soutenez le Téléthon !
Re: HttpClient, HttpURLConnection, server Authentication with encoding
Posted by Michael Becke <be...@u.washington.edu>.
Hi Ryan,
Try setting the credentials using the following:
client.getState().setCredentials(null, sUrl, new
UsernamePasswordCredentials(sServerAuthName, sServerAuthPassword));
Notice that null is not surrounded in quotes.
Mike
On Dec 13, 2004, at 6:45 AM, Ryan julius wrote:
> Hello,
>
> I have this String : sXmlMessage =
> "<mainMessage>messageBody</mainMessage>"
> I would like to send sXmlMessage to a server named: webMServer whose
> address is http://pyasmp:5126.
> webMServer use Basic authentication, the format of
> authentication(credentials!?!) is admin:servAdmin
> credentials and sXmlMessage must be encoded using BASE64Encoder.
> My question:
> 1.) How can I POST sXmlMessage to webMServer using HttpClient?
>
> Note: I have unsuccessfully tried the BasicAuhentication example, I
> still have this message:
>
> [WARN] HttpMethodBase - -No credentials available for the 'webMethods'
> authentication realm at pyasmp Method failed: HTTP/1.0 401
> [ISS.0084.9004] Access Denied
> 2.) As U can see in the following piece of code, I have also tried
> HttpUrlConnection; but there is no link with the HttpClient. What
> might be wrong with this code?
>
> Thanks.
>
> The code:
> =======
> client.getState().setCredentials("null", sUrl, new
> UsernamePasswordCredentials(sServerAuthName, sServerAuthPassword));
> HttpURLConnection httpUrlConnection = null;
> try {
> httpUrlConnection = (HttpURLConnection)
> url.openConnection();
> } catch (IOException e) {
> e.printStackTrace(); //To change body of catch statement
> use File | Settings | File Templates.
> }
> //Codage de la chaine de login et du fichier XML.
> String authentication = (new sun.misc.BASE64Encoder()).encode(
> (sServerAuthName + ":"+ sServerAuthPassword).getBytes() );
> httpUrlConnection.setRequestProperty("Authorization", "Basic
> "+ authentication );
> String sXmlMessageCoded = "";
> /*
> try {
> sXmlMessageCoded = URLEncoder.encode(sXmlMessage, "UTF-8");
> } catch (UnsupportedEncodingException e) {
> e.printStackTrace(); //To change body of catch statement
> use File | Settings | File Templates.
> }
> */
> sXmlMessageCoded = URLEncoder.encode(sXmlMessage);
>
> OutputStream out = null;
> try {
> out = httpUrlConnection.getOutputStream();
> out.write(sXmlMessageCoded.getBytes());
> } catch (IOException e) {
> e.printStackTrace(); //To change body of catch statement
> use File | Settings | File Templates.
> }
> String sInLine = null;
> String sInFullLine = null;
> BufferedReader inReader;
> try {
> inReader =
> new BufferedReader(
> new InputStreamReader(httpUrlConnection.getInputStream()));
> while ((sInLine = inReader.readLine()) != null) {
> sInFullLine+=sInLine;
> }
> } catch (IOException e1) {
> // TODO Bloc catch auto-généré
> e1.printStackTrace();
> }
> System.out.println("Résultat de la requête http : "+ new
> String(sInFullLine));
>
> //************************************************
> //************************************************
> HttpMethod method = null;
> // Relance de la requête si nécessaire ...
> DefaultMethodRetryHandler methodRetry = new
> DefaultMethodRetryHandler();
> methodRetry.setRetryCount(3);
> //En cas de succès d'envoi de requête, ne pas reessayer.
> methodRetry.setRequestSentRetryEnabled(false);
> // Create a method instance.
> if(sRequestMethod.equalsIgnoreCase("get")){
> method = new GetMethod(sUrl);
> ((GetMethod)method).setMethodRetryHandler(methodRetry);
> }else{
> //Target URL ...
> method = new PostMethod(sUrl);
> ((PostMethod)method).setMethodRetryHandler(methodRetry);
> ((PostMethod)method).setRequestBody(sXmlMessageCoded);
> }
> // Tell the GET method to automatically handle authentication.
> The
> // method will use any appropriate credentials to handle basic
> // authentication requests. Setting this value to false will
> cause
> // any request for authentication to return with a status of
> 401.
> // It will then be up to the client to handle the
> authentication.
> method.setDoAuthentication( true );
> try {
> // Execute the method.
> int statusCode = client.executeMethod(method);
> if (statusCode != HttpStatus.SC_OK) {
> System.err.println("Method failed: " + method.getStatusLine());
> }
> // Read the response body.
> byte[] responseBody = method.getResponseBody();
> // Deal with the response.
> // Use caution: ensure correct character encoding and is not
> binary data
> setOutput(OUT_RESPONSE, responseBody);
> logger.info(new String(responseBody));
> } catch (HttpException e) {
> System.err.println("Fatal protocol violation: " + e.getMessage());
> e.printStackTrace();
> } catch (IOException e) {
> System.err.println("Fatal transport error: " + e.getMessage());
> e.printStackTrace();
> } finally {
> // Release the connection.
> method.releaseConnection();
> }
> }
>
>
>
>
>
>
> ---------------------------------
> Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour
> vos mails !
> Créez votre Yahoo! Mail
>
> Avec Yahoo! faites un don et soutenez le Téléthon !
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org