My code in .net is as the following code. I want to write it in java. How can I do it? Shoul I use httpclient or socket to do this?
using (WebClient wc = new WebClient())
{
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("HOST", "example.com");
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
wc.Headers.Add("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3");
html = wc.DownloadString(link);
if (temp == null)
return string.Empty;
return html;
}
Use HttpUrlConnection as it comes with default JDK. No extra libraries are need to be downloaded.
Here is the translation for above piece of code in java
public static String get(String link){
HttpURLConnection connection=null;
try{
URL url=new URL(link);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("HOST", "example.com");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3");
connection.setDoInput(true);
connection.setDoOutput(true);
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String line,response="";
while((line=in.readLine())!=null)
response+=(line+"\n");
in.close();
return response;
}catch(Exception e){}
return "";
}
Related
I've this code in Java 8
String u = "https://test.com?WSDL";
URL url = new URL(u);
System.out.println("Link: " + u);
SSLContext sc = SSLContext.getInstance("TLSv1");
sc.init(null, null, new java.security.SecureRandom());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sc.getSocketFactory());
System.out.println("After url connection");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName("UTF-8")));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
}
br.close();
And I get Error 403 forbidden when I connect to a ii's web service. How can I get Internet explorer subcode error (es. 403.1 execute access forbidden, 403.2 Read access forbidden)?
I am trying to call an endpoint implemented in JAVA with below HttpURLConnection. if i put the path as my aplication running in localhost, everything works great.
But when this application is published into a server(Oracle Weblogic), I get the error:
java.io.IOException: Server returned HTTP response code: 500 for URL: path
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
This error is thrown in below block at the line with **
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
conn.setRequestProperty("Content-Language", "en-US");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "close");
OutputStream os = conn.getOutputStream();
os.write(new Gson().toJson(debtAccountList).getBytes("UTF-8"));
os.flush();
***InputStream in = new BufferedInputStream(conn.getInputStream());***
System.out.println(in.toString());
in.close();
os.close();
conn.disconnect();
}
What makes this works in localhost but not in deployed application?
I want to delete the API from url : "http://dublr024vm.devlab.ibm.com:60633/B2BiAPIs/svc/cadigitalcertificates/_id:tibco_ssl" , I am getting java.io.IOException: Server returned HTTP response code: 403 for URL. I have passed the correct authentication details too.
Below is the part of my code:
public static void call_me() throws Exception {
InputStream is = null;
String url = "http://dublr024vm.devlab.ibm.com:60633/B2BiAPIs/svc/cadigitalcertificates/_id:tibco_ssl";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
con.setDoOutput(true);
System.setProperty("http.agent", "Chrome");
// optional default is DELETE
con.setRequestMethod("DELETE");
//add request header
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
byte[] message = "admin:password".getBytes("UTF-8");
String encoding = DatatypeConverter.printBase64Binary(message);
con.setRequestProperty("Authorization", "Basic "+new String(encoding));
con.connect();
is = obj.openConnection().getInputStream();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'DELETE' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line, responseText = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
responseText += line;
}
br.close();
con.disconnect();
}
So, I want to get data from aspx page. And I need to do the follow steps
1)Send GET request and get __VIEWSTATE and __EVENTVALIDATION.
2)Send POST request with parameters (to login) then update __VIEWSTATE and __EVENTVALIDATION
3)Send POST request with parameters (to choose field) then update variables too
4)Send POST request with parameters (to press button)
5)Parse page content.
Page content dynamically loaded after script.
I tryed to use HttpUrlConnection
public String sendGet(String url) throws Exception {
StringBuilder result = new StringBuilder();
URL ur = new URL(url);
HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
connection.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
result.append(inputLine);
}
br.close();
return result.toString();
}
public String sendPost(String url, List<NameValuePair> formParams) throws Exception {
URL ur = new URL(url);
HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
connection.setReadTimeout(10000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(10000);
connection.setRequestMethod("POST");
for (HttpCookie cookie : this.cookies) {
connection.addRequestProperty("Cookie", cookie.toString());
}
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.connect();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(formParams));
writer.flush();
writer.close();
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = "";
String line;
while ((line = br.readLine()) != null) {
response += line;
}
System.out.println(Jsoup.parse(response).text());
updateViewState(response);
updateSubSession(response);
return response;
}
And I stayed in the second step. POST and GET subSession is different , but Cookie is the same :). So, EVENTVALIDATION and VIEWSTATE is invalid, and I get "login.aspx" page again. This is how I get Cookie
List<HttpCookie> cookies;
public void init() throws Exception {
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
String html = sendGet(url);
updateViewState(html);
updateSubSession(html);
CookieStore cookieJar = manager.getCookieStore();
cookies=cookieJar.getCookies();
}
So, I tryed to send POST with subSession.
public String sendPost(String url, List<NameValuePair> formParams) throws Exception {
URL ur = new URL(url+subSession);
HttpURLConnection connection = (HttpURLConnection) ur.openConnection();
And...
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.aogc2.state.ar.us:8080/DWClient/Login.aspx?DWSubSession=9852&v=1589
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at com.company.UrlConnection.sendPost(UrlConnection.java:111)
at com.company.UrlConnection.login(UrlConnection.java:138)
at com.company.UrlConnection.start(UrlConnection.java:35)
at com.company.Main.main(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
I thought that I was wrong to get cookie and I tryed to get cookie like this:
connection.getHeaderFields().get("Set-Cookie");
but Cookie were null .
Where I was wrong? Thank you a lot!
I'm trying to post a file from local to another platform over a switch. When i do it with DefaultHttpClient there is no problem.
HttpParams params = new BasicHttpParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(interfaceIp));
DefaultHttpClient httpClientPost = new DefaultHttpClient(params);
But i have to do it with HttpURLConnection. Is there a way to do this?
for example:
httpConn = (HttpURLConnection) url.openConnection(myHttpParams);
here is my HttpUrlConn codes
url = new URL(baseUrl+"/html/uploadimage.cgi");
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +"----WebKitFormBoundary"+boundary);
httpConn.setRequestProperty("Host", "192.168.1.1");
httpConn.setRequestProperty("Connection", "keep-alive");
httpConn.setRequestProperty("Content-Length", "16551361");
httpConn.setRequestProperty("Cache-Control", "max-age=0");
httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpConn.setRequestProperty("Origin", "http://192.168.1.1");
httpConn.setRequestProperty("Upgrade-Insecure-Requests", "1");
httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
httpConn.setRequestProperty("Referer", baseUrl + "/html/advance.html");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4");
String cookie = "Username="+ username +"; " +
"Password="+ cyreptedPassword +"; Language=tk; " +
"username="+ username +"; " +
"SessionID_R3="+ sessionID +"; activeMenuID=maintain_settings; activeSubmenuID=device_mngt";
httpConn.setRequestProperty("Cookie", cookie);
httpConn.setAllowUserInteraction(true);
httpConn.setConnectTimeout(9999*9999999);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
thanks for all.
HttpUrlConnection does not use http params, but it uses proxy.
here is some information about httpUrlConnection example.