I need to be able to use a password protected proxy and be able to read json information returned from a url.
I do not want to declare proxies at the system level; I would like to have multiple proxies being used in the same application.
What is the best way to do this?
I once faces the same problem. Unfortunately, JSoup is not a good choice for this. I ended up using the apache http client, which works nicely with proxies.
Here is the proxy-relevant part of my http-client configuration:
String ipStr = "the.proxy.ip.string";
int port = 8080;
String proxyLogin = "your login name";
String proxyPassword = "your password";
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(ipStr, port),
new UsernamePasswordCredentials(proxyLogin, proxyPassword));
HttpHost httpHost = new HttpHost(ipStr, port, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
You can use the http-client to get the website or JSON response from the net. If the content is HTML, you can use JSoup as parser with the returned input. If you get JSON back, then you probably want to use a JSON parser like json-simple (but there are many other very useful JSON libraries out there!)
Related
I am trying to consume a RESTFUL web service using Java(HttpURLConnection and InputStream).I am able to print the response using BufferedReader, but it returns a response header as well and the format is causing issues to convert it to a Java POJO.
I tried using a URLConnection and then retrieving the input stream and passing it to the ObjectMapping(provided by Jackson)
final URL url = new URL("url");
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("GET");
final ObjectMapper objectMapper = new ObjectMapper();
MyData myData = objectMapper.readValue(uc.getInputStream(), MyData.class);
Error Message : "No content to map due to end-of-input\n"
In your code you don't show where you actually read the data and where you declared and filled your output variable. As code is now it seems to be the incorrect reading from your rest service. But instead of writing your own code to read fro rest url I would suggest to use the 3d party library that does it for you. Here is few suggestions: Apache Http Client, OK Http client and finally my favorite - MgntUtils Http Client (library written and maintained by me) Here is the HttpClient javadoc, Here is the link to The latest Maven artifacts for MgntUtils library and here MgntUtils Github link that contains library itself with sources and javadoc. Choose some Http Client and read the content using that client and then you can use the content.
I have a url which needs authentication (like a browser pop up appears asking username and password.)
Generally, we can use the following format to achieve this:
http://username:password#url.com
Using RESTEasy client builder
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://url.com");
How to achieve it without having to construct the http://username:password#url.com myself? I've seen if there are any methods which I could use to set it with no luck. Also I'm not sure how these credentials are passed, headers, cookies etc..
Looks like Basic Authentication. If that's the case, you just need to set the Authorization header to Basic base64encode(username:password).
For example:
String credentials = "username:password"
String base64encoded = Base64.getEncoder().encodeToString(credentials.getBytes());
Response response = target.request()
.header(HttpHeaders.AUTHORIZATION, "Basic " + base64encoded)....
The Base64 class I used is from Java 8. If you're not using Java 8, there are libraries that have Base64 support. Prior to Java 8, there's a com.sun internal Base64 class, but it's advised not to use those.
If you just want to run a quick test (and don't have Java 8 or don't want to go looking for a lib), you can go to this site and just type in your username:password and encode it, then just copy and paste to your code.
For example:
base64encode(username:password) == dXNlcm5hbWU6cGFzc3dvcmQ=
So I ran into a problem and the service support asked me to support them with the HTML.
So I need to get the equivalent HTML that is generated by
HttpClient client = new HttpClient();
PostMethod post = new PostMethod( "https://www.url.to/post-to" );
NameValuePair[] params = {
new NameValuePair( "NAME", name ),
new NameValuePair( "EMAIL", email ),
for( NameValuePair param : params ){
post.setParameter(param.getName(), param.getValue());
}
Is it possible to get it as HTML or get the request sent as a string, with all the headers etc?
If you use some logging framework, Apache HttpClient can be configured to log requests and responses which go through it, see Wire logging. Judging by the documentation, if logging framework is properly setup, this should be enough to enable it
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
EDIT
These settings are ok if you use Apache Commons Logging as logging framework. Above mentioned link also contains examples for setting this up for log4j and java.util.logging.
You could write an equivalent HTML form, that sends the same two parameters to the same URL.
However, if you're opening a support ticket for a problem caused by this connection, an HTML will probably not be very helpful, because a browser may be sending different headers to the URL even if the request body is the same.
You can get the request headers from the post variable, using post.getRequestHeaders(). It returns an array of type Header[], which you'll have to translate into strings. The message body is probably easy to reconstruct from the parameters by urlencoding the values. The body is always in the form NAME=urlencodedname&EMAIL=urlencodedemail.
I'm writing class to read from a JSON web service using Jackson. Previously, when reading from a web service I've used a custom web browser class to be able to set certain connection information, such as proxy host/port/username/password, etc as well as read and connection timeout values.
Is there a way to do this in Jackson natively? E.g. by setting the proxy parameters in a configuration?
Or should I revert back to getting the API response as a string and then using Jackson to parse it?
FYI, this is the (simplified) code that I am using.
URL configUrl = new URL("http://my.webservice.com/api");
ConfigClass localConfig = mapper.readValue(configUrl, ConfigClass.class);
I would retrieve the api response as a Reader (or InputStream), and then use Jackson to parse that. Jackson just calls configUrl.openStream() under the hood, and there's no reason not to do that yourself.
I think you should do the latter, proxy support hasn't been added to Jackson.
Plus it's pretty simple, using the Proxy class.
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
URL url = new URL("URL");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
I obviously don't know a ton about cookies, if they'd even be compatible, etc, etc. If this is possible, can someone show an example of how?
I'm not quite sure what you mean by "use Firefox cookies". Here's a snipet of code that I wrote to grab data from the Goolge Reader API, by passing a cookie along with the Apache HttpClient request.
It needs to send a cookie that Google gives you upon connection ("SID") along with its request. Is this what you are looking for?
// Obtain SID
GetMethod getLogin = new GetMethod("https://www.google.com/accounts/ClientLogin?service=reader&Email="+
login+"&Passwd="+password);
client.executeMethod(getLogin);
String loginText = getLogin.getResponseBodyAsString();
// The value of SID is loginText.substring (4,firstLineLength)
BufferedReader reader = new BufferedReader(new StringReader(loginText));
String SID = loginText.substring(4, reader.readLine().length() );
GetMethod grabData = new GetMethod("http://www.google.com/reader/atom/user/-/state/com.google/broadcast");
grabData.setRequestHeader("Cookie", "SID="+SID);
client.executeMethod(grabData);