i have url data access. data in text form , password protected...but here's thing...it password protected different website. have spent weeks on issue. when use apache httpclient, can log login url fine, cannot figure out how gain access data url. every time try gain access data url, http 500 error. suggestions issue? don't think common problem considering have not come across in many stackoverflow , google searches. thank if can :)
below example of 1 of programs have tried using no avail...(some of information private, changed username, password, , url)
package apache1; import org.apache.http.auth.authscope; import org.apache.http.auth.usernamepasswordcredentials; import org.apache.http.client.credentialsprovider; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.basiccredentialsprovider; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.util.entityutils; import java.net.*; import java.io.*; /** * simple example uses httpclient execute http request against * target site requires user authentication. */ public class apache2 { public static void main(string[] args) throws exception { credentialsprovider credsprovider = new basiccredentialsprovider(); credsprovider.setcredentials( new authscope("localhost", 443), new usernamepasswordcredentials("myusername", "mypw")); closeablehttpclient httpclient = httpclients.custom() .setdefaultcredentialsprovider(credsprovider) .build(); try { httpget httpget = new httpget("login url"); system.out.println("executing request " + httpget.getrequestline()); closeablehttpresponse response = httpclient.execute(httpget); try { system.out.println("----------------------------------------"); system.out.println(response.getstatusline()); entityutils.consume(response.getentity()); url oracle = new url("data url"); urlconnection yc = oracle.openconnection(); bufferedreader in = new bufferedreader(new inputstreamreader( yc.getinputstream())); string inputline; while ((inputline = in.readline()) != null) system.out.println(inputline); in.close(); } { response.close(); } } { httpclient.close(); } } } here recent code handle cookies. keep getting http 500 error.
package apache1; //import org.apache.http.*; import org.apache.http.auth.authscope; import org.apache.http.auth.usernamepasswordcredentials; import org.apache.http.client.credentialsprovider; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.basiccredentialsprovider; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.util.entityutils; import java.net.*; import java.io.*; /** * simple example uses httpclient execute http request against * target site requires user authentication. */ public class apache2 { public static void main(string[] args) throws exception { credentialsprovider credsprovider = new basiccredentialsprovider(); credsprovider.setcredentials( new authscope("localhost", 443), new usernamepasswordcredentials("myusername", "mypassword")); closeablehttpclient httpclient = httpclients.custom() .setdefaultcredentialsprovider(credsprovider) .build(); try { httpget httpget = new httpget("login_website"); system.out.println("executing request " + httpget.getrequestline()); closeablehttpresponse response = httpclient.execute(httpget); try { system.out.println("----------------------------------------"); system.out.println(response.getstatusline()); entityutils.consume(response.getentity()); url oracle = new url("data_website"); urlconnection yc = oracle.openconnection(); bufferedreader in = new bufferedreader(new inputstreamreader( yc.getinputstream())); string inputline; while ((inputline = in.readline()) != null) system.out.println(inputline); in.close(); } { response.close(); } } { httpclient.close(); } } }
Comments
Post a Comment