Get the result of POST in Java

background

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.

Preparation

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 inquiry results

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));
	}

}

Execution result

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

Get the result of POST in Java
Get the URL of the HTTP redirect destination in Java
[Java] Get the file in the jar regardless of the environment
[Java] Get the day of the specific day of the week
How to get the date in java
The story of writing Java in Emacs
[Java] Get the dates of the past Monday and Sunday in order
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
POST JSON in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
Feel the passage of time even in Java
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
Get EXIF information in Java
Return the execution result of Service class in ServiceResponse class
Get the name of the test case in the JUnit test class
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
[Java] Get KClass in Java [Kotlin]
[Java] Get the file path in the folder with List
POST Json in Java ~ HttpURLConnection ~
Change the storage quality of JPEG images in Java
Implementation of gzip in java
Get the URL of the HTTP redirect destination in Ruby
Implementation of tri-tree in Java
Conditional branching of the result of SQL statement to search only one in Java
Summarize the additional elements of the Optional class in Java 9
Sample code to get the values of major SQL types in Java + MySQL 8.0
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to get the class name / method name running in Java
Was done in the base year of the Java calendar week
[Java] Throw a request and display the screen ② (GET / POST)
A quick explanation of the five types of static in Java
Get the next business day after the specified date in JAVA
Get to the abbreviations from 5 examples of iterating Java lists
[Java] Throw a request and display the screen (GET / POST)
Count the number of digits after the decimal point in Java
How to derive the last day of the month in Java
Be sure to compare the result of Java compareTo with 0
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
[Java version] The story of serialization
Specify the java location in eclipse.ini
Get Null-safe Map values in Java
Unzip the zip file in Java
OkHttp3 (GET, POST) in Android Studio
List of members added in Java 9
Parsing the COTOHA API in Java
Get the ID of automatic numbering
Get stuck in a Java primer
List of types added in Java 9