I try to send POST data to a Website. But the "login" will not work.
Slowly i dont know why. There are quite a lot of ways to login to a website without browser-ui. What is the "best" method to login here ?
I uses Jsoup and "normal" HttpURLConnection. With Selenium it works fine, but very slow =(
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.List;
import java.util.Map;
public class postget {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://de.metin2.gameforge.com/";
private static final String POST_URL = "https://de.metin2.gameforge.com:443/user/login?__token=";//+token
private static final String POST_PARAMS = "username=USERNAME"+"password=PASSWORD";
static final String COOKIES_HEADER = "Set-Cookie";
public static void main(String[] args) throws IOException {
String var = sendGET();
String var1 =var.substring(0,var.indexOf(";"));
String var2 =var.substring(var.indexOf(";")+1,var.indexOf(":"));
String token =var.substring(var.indexOf(":")+1);
//sendPOST(token,cookie);
jsoup(cookie(),token);
}
private static String sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String veri1=response.substring(response.indexOf("verify-v1")+20,response.indexOf("verify-v1")+63);
String veri2=response.substring(response.indexOf("verify-v1")+104,response.indexOf("verify-v1")+147);
String token=response.substring(response.indexOf("token")+6,response.indexOf("token")+38);
return (veri1+";"+veri2+":"+token);
} else {
System.out.println("GET request not worked");
return " ";
}
}
private static String cookie() throws IOException{
String realCookie="";
URL obj = new URL(GET_URL);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
java.net.CookieManager msCookieManager = new java.net.CookieManager();
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
}
}
List<HttpCookie> cookiess = msCookieManager.getCookieStore().getCookies();
if (cookiess != null) {
if (cookiess.size() > 0) {
for (HttpCookie cookie : cookiess) {
realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
return(realCookie);
}
}
}
return(realCookie);
}
private static void sendPOST(String token,CookieManager msCookieManager) throws IOException {
URL obj = new URL(POST_URL+token);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
if (msCookieManager != null) {
List<HttpCookie> cookies = msCookieManager.getCookieStore().getCookies();
if (cookies != null) {
if (cookies.size() > 0) {
for (HttpCookie cookie : cookies) {
String realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
}
con.setRequestProperty("Cookie", StringUtils.join(cookies, ";"));
}
}
}
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
if(response.indexOf("form-login")>0){
System.out.println("NOPE");
}
if(response.indexOf("logout")>0){
System.out.println("YES");
}
} else {
System.out.println("POST request not worked");
}
}
private static void jsoup(String cookie,String token) throws IOException{
Document response = Jsoup.connect("https://de.metin2.gameforge.com/")
.referrer("https://de.metin2.gameforge.com/main/index?__token=5"+token)
.cookie("Cookie","gf-locale=de_DE; _ga=GA1.2.404625572.1501231885; __auc=a02de56115d8864ad2bd7ad8120; pc_idt=ANu-cNegKMzU8CAsBQAHIu1cRlXEEUD1ka6NvJXWqO4sVVaLIsqSFPyZFt8dXHKcrhrB_u2FFdQnD-2vsA377NnVgjkrKn-3qzi5Q3LXbzgnbbmIEir4zYNCddPbjCUg9cVpSU4GP-CvU53XhrQ6_MWP9tOYNdjCqRVIPw; SID="+cookie+"; __utma=96667401.404625572.1501231885.1503225538.1503232069.15; __utmc=96667401; __utmz=96667401.1501247623.3.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)")
//.data("X-DevTools-Emulate-Network-Conditions-Client-Id","c9bbb769-df80-47ff-8e25-e582de026ecc")
.userAgent("Mozilla")
.data("username", "USERNAME")
.data("password", "PASSWORD")
.post();
System.out.println(response);
}
}
}
The var1 and var1 are unnecessary.
They was my first idea to send with my POST.
Here is a picture if the login-form:
And of HttpClient
Related
When I try and send a request to my server, the server only gets POST requests no matter if I set setRequestMethod("GET") . This is the function I am calling, with an url and 2 params I need to send with them:
public static String getHTML(String urlToRead,String urlParameters) {
try {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream (
conn.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
catch (Exception e) {
return e.getMessage().toString();
}
}
}
Any help is welcome or any other functions to be able to send a GET request to a server by sending the URL and two parameters.
Do it as follows:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
class Main {
public static void main(String[] args) {
System.out.println(getHTML("http://localhost:8080/TestDynamicProject/getdata.do?t1=tp1&t2=tp2"));
}
public static String getHTML(String urlToRead) {
try {
StringBuilder result = new StringBuilder();
InputStream stream = null;
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = conn.getInputStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
} catch (Exception e) {
return e.getMessage().toString();
}
}
}
Output:
Response from GET | Request parameters: t1=[tp1],t2=[tp2]
Given below is my Servlet:
#WebServlet("/getdata.do")
public class TestServlet extends HttpServlet {
public TestServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
request.getParameterMap().forEach((k, v) -> sb.append(k.toString() + "=" + Arrays.toString(v) + ","));
sb.deleteCharAt(sb.length() - 1);
response.getWriter().append("Response from GET").append(" | Request parameters: ").append(sb);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Response from POST");
}
}
I'm tring to connect "https://api.tdax.com/api/orders/?pair=btc_thb"
this url is working on chrome, postman.
I can connect this url with C#.
But ca'nt connect with java.
namespace Exchanges.Satang
{
class SatangApi
{
private static class WebApi
{
private static readonly HttpClient st_client = new HttpClient();
static WebApi()
{
st_client.Timeout = TimeSpan.FromSeconds(2);
}
public static HttpClient Client { get { return st_client; } }
public static string Query(string url)
{
var resultString = Client.GetStringAsync(url).Result;
return resultString;
}
}
public static string GetOrders(string symbol)
{
const string queryStr = "https://api.tdax.com/api/orders/?pair=";
var response = WebApi.Query(queryStr + symbol);
return response.ToString();
}
}
}
this C# code working well
but following java code not working, get 403 error.
private String publicOperation(String operation) throws IOException, BadResponseException {
StringBuilder result = new StringBuilder();
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//con.setRequestProperty("Content-Type", "application/json");
con.setRequestMethod("GET");
//https://api.tdax.com/api/orders/?pair=btc_thb
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
Some servers expect a User-Agent header to be present in the request to consider it as a valid request. So you need to add that to your request as follows.
con.setRequestProperty("User-Agent", "My-User-Agent");
int responseCode = con.getResponseCode();
The value of this header (My-User-Agent in the above example) can be set to any String you desire for this endpoint. For example, Postman sets something like PostmanRuntime/7.16.3 for this.
C# might be doing this internally, so you didn't have to set it explicitly.
public String getOrders(SatangCurrencyPairs currencyPair) throws IOException, BadResponseException {
String operation="orders/?pair="+currencyPair.toString();
StringBuilder result = new StringBuilder();
URL url = new URL(baseUrl+operation);
//URL url_ = new URL("https://api.tdax.com/api/orders/?pair=btc_thb");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "java client");
con.setRequestMethod("GET");
//https://api.tdax.com/api/orders/?pair=btc_thb
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
This is my java code i am repeating the same steps for url and url1 so i want make a function in which i place the my url code seperate and url1 code seperate then call it in a main class. First I want to access String url and then I want to access String url1.As I am new in java so I am new in java so I am not able to enclose it into function
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
public class Test_URL_Req {
public static void main(String[] args){
// TODO Auto-generated method stub
try {
String id ="301";
String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/definitions?api-version=4.1";
String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + id +"&resultFilter=succeeded&$top=1";
URL obj = new URL(url);
URL obj1 = new URL(url1);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection();
int responseCode = con.getResponseCode();
int responseCode1 = con1.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
System.out.println("\nSending 'GET' request to URL : " + url1);
System.out.println("Response Code : " + responseCode1);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
BufferedReader in1 = new BufferedReader(
new InputStreamReader(con1.getInputStream()));
String inputLine;
String inputLine1;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
//System.out.println(response);
}
StringBuffer response1 = new StringBuffer();
while ((inputLine1 = in1.readLine()) != null) {
response1.append(inputLine1);
//System.out.println(response1);
}
in.close();
JSONObject obj_JSONObject = new JSONObject(response.toString());
JSONObject obj_JSONObject1 = new JSONObject(response1.toString());
JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");
JSONArray obj_JSONArray1 = obj_JSONObject1.getJSONArray("value");
for(int i=0; i<obj_JSONArray.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);
String value = obj_JSONObject2.getString("name");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch= "DEVOPS";
if(value.equals(toSearch)){
System.out.println("STATUS:-");
System.out.println(value);
String result =obj_JSONObject2.getString("name");
System.out.println("BUILD NAME");
System.out.println(result);
String Def_id = obj_JSONObject2.get("id").toString();
System.out.println("DEFINATION ID");
System.out.println(Def_id);
break;
}
}
for(int i=0; i<obj_JSONArray1.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray1.getJSONObject(i);
String value = obj_JSONObject2.getString("result");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch1= "succeeded";
if(value.equals(toSearch1)){
System.out.println("#######################################");
System.out.println("RESULT");
System.out.println(value);
String result =obj_JSONObject2.getString("status");
System.out.println("STATUS");
System.out.println(result);
String Def_id = obj_JSONObject2.get("id").toString();
System.out.println("BUILD ID");
System.out.println(Def_id);
boolean keepForever =obj_JSONObject2.getBoolean("keepForever");
if(keepForever == false)
{
keepForever=true;
}
System.out.println(keepForever);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
public static String getURLResponse( String url){
try {
System.out.println("\nSending 'GET' request to URL : " + url);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
//System.out.println(response);
}
in.close();
return response.toString();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
In main method -
public static void main(String[] args){
String url = "....";
String url1 =".....";
String response = getURLResponse(url);
String response1 = getURLResponse(url1);
JSONObject obj_JSONObject = new JSONObject (response);
JSONObject obj_JSONObject1 = new JSONObject(response1);
...
}
Create a method that takes a String and it appears you want a StringBuffer response...
public StringBuffer doSomething(String url){
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
//etc
return response;
}
And just pass your two URLs to it from the main:
String url = "https://tfs.tpsonline...
StringBuffer response = doSomething(url);
My question is how to add basic auth Java Android AsyncTask? Some of developers said it needs to be declared in RequestHandler.java or in doInBackground AsyncTask function. Below is my code:
private void loginTask(String _username, String _password){
final String username = _username;
final String password = _password;
class LoginTask extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginActivity.this,"Fetching...","Wait...",false,false);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
}
#Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(App.URL_AUTHENTICATION);
Toast.makeText(LoginActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
return s;
}
}
LoginTask gt = new LoginTask();
gt.execute();
}
RequestHandler class: https://github.com/IntellijSys/AndroidToDoList/blob/master/app/src/main/java/my/intellij/androidtodolist/RequestHandler.java
Try this
RequestHandler rh = new RequestHandler();
// your basic auth username and password
rh.setBasicAuth("username","password");
String s = rh.sendGetRequest(App.URL_AUTHENTICATION);
RequestHandler class with Basic auth
import android.util.Base64;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
/**
* Created by ZERO on 16/08/2016.
*/
public class RequestHandler {
private String username;
private String password;
//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//set Basic auth
processBasicAuth(conn);
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private void processBasicAuth(HttpURLConnection conn) {
if (username != null && password != null) {
try {
String userPassword = username + ":" + password;
byte[] data = userPassword.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
conn.setRequestProperty("Authorization", "Basic " + base64);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public String sendGetRequest(String requestURL) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//set Basic auth
processBasicAuth(con);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s + "\n");
}
} catch (Exception e) {
}
return sb.toString();
}
public String sendGetRequestParam(String requestURL, String id) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL + id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s + "\n");
}
} catch (Exception e) {
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
public void setBasicAuth(String username, String password) {
this.username = username;
this.password = password;
}
}
I have a servlet online that I'm trying to contact in order to do some basic testing. This is the servlet code:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class index extends HttpServlet {
private static final long serialVersionUID = 1L;
public index() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
long time1 = System.currentTimeMillis();
long time2 = time1 + 10000;
out.println(time1);
long i = 400000000l;
while (System.currentTimeMillis() < time2) {
i++;
}
out.print(time2);
}
}
Now, I'm trying to get information from the server using the following code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequest {
public static void main(String args[]) {
BufferedReader rd;
OutputStreamWriter wr;
try {
URL url = new URL("http://blahblahblah/index");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
conn.setConnectTimeout(50000);
rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
However I keep getting the same 405 error. What am I doing wrong?
What you are seeing is the HttpServlet's default implementation of doPost(), since you don't override it in your index servlet.
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
which immediately sends a 405 response.
This occurs because you call
conn.getOutputStream()
which makes the URLConnection think you are sending a POST by default, not the GET you are expecting. You aren't even using the OutputStream so why open it, then flush() it and never us it again?
wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
You can also put a check if the request method is GET or POST:
private static final int READ_TIMEOUT = 100000;
private static final int CONNECT_TIMEOUT = 150000;
public static final String POST = "POST";
public static final String GET = "GET";
public static final String PUT = "PUT";
public static final String DELETE = "DELETE";
public static final String HEAD = "HEAD";
private URL url = null;
private HttpURLConnection conn = null;
private OutputStream os = null;
private BufferedWriter writer = null;
private InputStream is = null;
private int responseCode = 0;
private String request(String method, String url, List<NameValuePair> params) throws IOException {
if(params != null && method == GET){
url = url.concat("?");
url = url.concat(getQuery(params));
}
this.url = new URL(url);
conn = (HttpURLConnection) this.url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
if(params != null && method == POST){
os = conn.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
}
conn.connect();
responseCode = conn.getResponseCode();
is = conn.getInputStream();
String contentAsString = getStringFromInputStream(is);
return contentAsString;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params){
if (first){
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(pair.getName(),"UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(),"UTF-8"));
}
return result.toString();
}