This is a memo when you want to POST data from Java to a website and get the result. This time, we will POST the slip number to Yamato Transport's Luggage Inquiry System and obtain the inquiry result.
Examine the name attribute of the data to POST from the source of the parcel query system.
view-source
<td align="left">Detailed information
<input type="radio" name="number00" value="1" checked>Yes
<input type="radio" name="number00" value="2">None
</td>
···abridgement···
<td class="input">
<input name="number01" size="20" maxlength="14">
</td>
Select show / hide detailed information from source Radio button name is number00 You can see that the name of the text box for entering the slip number is number01 ~ number10.
Get it with the following code.
Post.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Post {
public String getResultPage(String postData, String postURL){
try{
String data = postData;
URL url = new URL(postURL);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.setDoOutput(true); //Flag to get the OutputStream from the generated URL connection
uc.setRequestMethod("POST");
OutputStream os = uc.getOutputStream();
os.write(data.getBytes());
os.flush();
os.close();
InputStream is = uc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"Shift_JIS"));
String line;
StringBuffer buf = new StringBuffer();
while((line = reader.readLine()) != null){
buf.append(line + "\n");
}
is.close();
uc.disconnect();
return buf.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String url = "https://toi.kuronekoyamato.co.jp/cgi-bin/tneko";
String query = "number00=1&number01=Slip number"; //* When POSTing data, name=value&name=value...Will be in the form
System.out.println(new Post().getResultPage(query,url));
}
}
The result of POST is output
python
···abridgement···
<table class="meisai">
<tr>
<th width="55"><br></th>
<th>Luggage status</th>
<th>Date</th>
<th>Times of Day</th>
<th>Store name in charge</th>
<th>Store code</th>
</tr>
<tr class="odd">
<td class="image"><img src="/images/ya_02.gif" alt="Progress"></td>
<td>Shipping</td>
<td>02/21</td>
<td>13:47</td>
<td><a href="http://sneko2.kuronekoyamato.co.jp/sneko2/Sngp?ID=NET_C&JC=031902&DN=&MD=&F=2" target="_blank">Tokyo Corporate Sales Branch</a></td>
<td>031902</td>
</tr>
<tr class="even">
<td class="image"><img src="/images/ya_02.gif" alt="Progress"></td>
<td>Delivery schedule</td>
<td>02/22</td>
<td>12:30</td>
<td>Nearest sales office</td>
<td>Center code of the nearest sales office</td>
</tr>
<tr class="odd">
<td class="image"><img src="/images/ya_02.gif" alt="Progress"></td>
<td>Delivery schedule</td>
<td>02/22</td>
<td>17:37</td>
<td>Nearest sales office</td>
<td>Center code of the nearest sales office</td>
</tr>
<tr class="even">
<td class="image"><img src="/images/nimotsu_01.gif" alt="latest"></td>
<td>Delivery completed</td>
<td>02/22</td>
<td>19:11</td>
<td><a href="Link to the nearest sales office" target="_blank">Nearest sales office</a></td>
<td>Center code of the nearest sales office</td>
</tr>
</table>
···abridgement···
that's all.
Recommended Posts