| import java.io.ByteArrayOutputStream; |
| import java.io.InputStream; |
| import java.io.UnsupportedEncodingException; |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| import org.apache.commons.httpclient.Credentials; |
| import org.apache.commons.httpclient.HttpClient; |
| import org.apache.commons.httpclient.UsernamePasswordCredentials; |
| import org.apache.commons.httpclient.auth.AuthScope; |
| import org.apache.commons.httpclient.methods.GetMethod; |
| import org.apache.commons.httpclient.util.EncodingUtil; |
| |
| import de.img.network.util.ReverseProxyBean; |
| import de.img.network.util.WebRetrievalException; |
| |
| public class MyMail { |
| private HttpClient client; |
| |
| private ReverseProxyBean reverseProxyBean; |
| private String urlMyMail = "http://x.x.x.x/mail/ajanssen.nsf/($All)/doUnid?open&ui=webmail"; |
| |
| |
| private byte[] responseBody = null; |
| |
| public MyMail () { |
| client = new HttpClient(); |
| setReverseProxyBean (new ReverseProxyBean()); |
| } |
| |
| public void setReverseProxyBean(ReverseProxyBean myReverseProxyBean) { |
| |
| reverseProxyBean = myReverseProxyBean; |
| reverseProxyBean.setProxyHost("x.x.x.x"); |
| reverseProxyBean.setProxyPort(8080); |
| reverseProxyBean.setProxyUsername("name"); |
| reverseProxyBean.setProxyPassword("password"); |
| } |
| |
| |
| |
| |
| |
| public void readMyMail() throws WebRetrievalException { |
| |
| buildRequestHeader(); |
| GetMethod get = new GetMethod(urlMyMail); |
| |
| |
| try { |
| |
| InputStream is = downloadXml(get); |
| String responseCharSet = get.getResponseCharSet(); |
| System.out.println("responseCharSet=" + responseCharSet); |
| ByteArrayOutputStream outstream = new ByteArrayOutputStream( |
| get.getResponseContentLength()> 0 ? (int) get.getResponseContentLength() : 4 * 1024); |
| byte[] buffer = new byte[4096]; |
| int len; |
| while ((len = is.read(buffer)) > 0) { |
| outstream.write(buffer, 0, len); |
| } |
| outstream.close(); |
| |
| this.responseBody = outstream.toByteArray(); |
| String val = EncodingUtil.getString(responseBody, responseCharSet); |
| System.out.println("val=" + val); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| |
| |
| private void buildRequestHeader() { |
| client.getParams().setAuthenticationPreemptive(true); |
| client.getHostConfiguration().setProxy(reverseProxyBean.getProxyHost(), reverseProxyBean.getProxyPort()); |
| Credentials credsProxy = new UsernamePasswordCredentials( |
| reverseProxyBean.getProxyUsername(), |
| reverseProxyBean.getProxyPassword()); |
| client.getState().setProxyCredentials(AuthScope.ANY, credsProxy); |
| |
| Credentials defaultcreds = new UsernamePasswordCredentials( |
| "nameServer", "pwdServer"); |
| client.getState().setCredentials(AuthScope.ANY, defaultcreds); |
| |
| |
| } |
| |
| private InputStream downloadXml(GetMethod get) throws WebRetrievalException { |
| String errMsg = ""; |
| try { |
| client.executeMethod(get); |
| if (get.getStatusCode() != 200) { |
| errMsg = "Die URL ist nicht erreichbar:" + get + ". (http-Statuscode=" + get.getStatusCode(); |
| |
| throw new WebRetrievalException(errMsg); |
| } |
| return get.getResponseBodyAsStream(); |
| |
| } |
| catch (Exception e) { |
| throw new WebRetrievalException("Die URL ist nicht erreichbar:" + get, e); |
| |
| } |
| |
| } |
| |
| |
| public static void main (String [] ars) { |
| try { |
| MyMail myMail = new MyMail(); |
| myMail.readMyMail(); |
| System.out.println(myMail.responseBody); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| |
| |
| |
| } |