package com.Bit4Tat;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.google.gson.Gson;
{
private static final String CHECK_BALANCE = "https://mtgox.com/code/getFunds.php?";
private static final String BUY_ORDER = "https://mtgox.com/code/buyBTC.php?";
private static final String SELL_ORDER = "https://mtgox.com/code/sellBTC.php?";
private static final String GET_ORDERS = "https://mtgox.com/code/getOrders.php?";
private static final String CANCEL_ORDER = "https://mtgox.com/code/cancelOrder.php?";
private static final String SEND_BTC = "https://mtgox.com/code/withdraw.php?";
private static final String CHECK_TICKER = "https://mtgox.com/api/0/data/ticker.php";
private ResponseContainer response;
{
user = username;
pass = password;
}
@Override
{
HttpsURLConnection conn = setupConnection(CHECK_BALANCE);
try {
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass.trim(), "UTF-8");
StringBuffer returnString = new StringBuffer();
try {
DataOutputStream wr = new DataOutputStream( conn.getOutputStream() );
int queryLength = data.length();
wr.writeBytes( data);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
try
{
response = new ResponseMtGox();
response.parseCheckBalance(rd);
}catch(Exception ex){
System.out.println("Error filling MtGox json in getResponse().");
ex.printStackTrace();
}
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
returnString.append(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
conn.disconnect();
return response;
}
@Override
{
StringBuffer returnString = new StringBuffer();
try {
URL url = new URL(CHECK_TICKER);
BufferedReader rd = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
returnString.append(line);
}
rd.close();
}catch (Exception e) {
e.printStackTrace();
}
return response;
}
@Override
public void buy(
double amount,
double price)
{
HttpsURLConnection conn = setupConnection(BUY_ORDER);
try
{
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
data += "&" + URLEncoder.encode("amount", "UTF-8") + "=" + URLEncoder.encode(Double.toString(amount), "UTF-8");
data += "&" + URLEncoder.encode("price", "UTF-8") + "=" + URLEncoder.encode(Double.toString(price), "UTF-8");
getResponse(data, conn);
}
catch (Exception e)
{
e.printStackTrace();
}
conn.disconnect();
}
@Override
public void sell(
double amount,
double price)
{
HttpsURLConnection conn = setupConnection(SELL_ORDER);
try
{
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
data += "&" + URLEncoder.encode("amount", "UTF-8") + "=" +
URLEncoder.encode(new java.text.DecimalFormat("0.00").format(amount), "UTF-8");
data += "&" + URLEncoder.encode("price", "UTF-8") + "=" +
URLEncoder.encode(new java.text.DecimalFormat("0.00").format(price), "UTF-8");
getResponse(data, conn);
}
catch (Exception e)
{
e.printStackTrace();
}
conn.disconnect();
}
@Override
{
HttpsURLConnection conn = setupConnection(CANCEL_ORDER);
try
{
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
data += "&" + URLEncoder.encode("amount", "UTF-8") + "=" + URLEncoder.encode(Integer.toString(orderID), "UTF-8");
getResponse(data, conn);
}
catch (Exception e)
{
e.printStackTrace();
}
conn.disconnect();
}
@Override
{
HttpsURLConnection conn = setupConnection(GET_ORDERS);
try
{
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
getResponse(data, conn);
}
catch (Exception e)
{
e.printStackTrace();
}
conn.disconnect();
}
String
getResponse (String data, HttpsURLConnection conn)
{
StringBuffer returnString = new StringBuffer();
try {
DataOutputStream wr = new DataOutputStream( conn.getOutputStream() );
int queryLength = data.length();
wr.writeBytes( data);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
returnString.append(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return returnString.toString();
}
{
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {}
@Override
}
{
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
try {
ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLContext.setDefault(ctx);
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpsURLConnection conn = null;
try {
conn = (HttpsURLConnection) url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setHostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String arg0, SSLSession arg1)
{
return true;
}
});
return conn;
}
}