/*
* Copyright (c) 2010 Andy Aspell-Clark
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package uk.org.aspellclark.fillerup.mid;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.util.Log;
/**
*
* @author andy
*/
public class MotorInsuranceDatabase {
/**
* check out
http://www.askmid.com/ownvehicle/
http://ownvehicle.askmid.com/
http://ownvehicle.askmid.com/askmid.aspx?regno=ynb29x&acceptTC=acceptTC
aspx form
<form name="Form1" method="post" action="askmid.aspx" id="Form1">
<input name="regno" type="text" maxlength="20" id="regno" tabindex="1" class="inputbox" />
<input id="acceptTC" type="checkbox" name="acceptTC" tabindex="2" />
</form>
*/
/*
see http://hc.apache.org/httpclient-3.x/methods/post.html
http://www.innovation.ch/java/HTTPClient/emulating_forms.html
*/
private static final String TAG = "MotorInsuranceDatabase";
/**
* from http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
*/
public Boolean askmid(String vehicleRegNbr) {
Boolean isInsured = Boolean.FALSE;
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
try {
// URL of CGI-Bin script.
url = new URL("http://ownvehicle.askmid.com/askmid.aspx");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput(true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput(true);
// No caching, we want the real thing.
urlConn.setUseCaches(false);
// Specify the content type.
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream());
String content = "regno=" + URLEncoder.encode(vehicleRegNbr) + "&acceptTC=" + URLEncoder.encode("1");
printout.writeBytes(content);
printout.flush();
printout.close();
// Get response data.
input = new DataInputStream(urlConn.getInputStream());
String str;
while (null != ((str = input.readLine()))) {
System.out.println(str);
//textArea.appendText(str + "\n");
}
input.close();
} catch (MalformedURLException mfUrl) {
Log.e(TAG, mfUrl.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return isInsured;
}
}
|