接着上一篇,这里介绍的是POST方法:
package retina;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import AiLab.base64_no_empty;
import net.sf.json.JSONObject;
public class action {
public static void main(String[] args) {
}
//将返回的ASCII码转为汉字(若有)
public static String ascii2native ( String asciicode ){
String[] asciis = asciicode.split ("\\\\u");
String nativeValue = asciis[0];
try
{
for ( int i = 1; i < asciis.length; i++ )
{
String code = asciis[i];
nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
if (code.length () > 4)
{
nativeValue += code.substring (4, code.length ());
}
}
}
catch (NumberFormatException e)
{
return asciicode;
}
return nativeValue;
}
/*请求,上传图片
* FileR、FileL:base64格式左右眼图片
* PiCi:批次号,用于区别上传接口参数patientID前缀
* f:区别上传接口参数examPhase
* */
public static String UpLoadPic(String FileR,String FileL,String PiCi,int f) throws IOException, NoSuchAlgorithmException, KeyManagementException {
StringBuffer sb = new StringBuffer("");
//图片转换base64,左眼右眼
String rightImage=base64_no_empty.GetBase64FromFile(FileR);
String leftImage=base64_no_empty.GetBase64FromFile(FileL);
HttpsURLConnection.setDefaultHostnameVerifier(new action().new NullHostNameVerifier());
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
try {
//设置代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxxxx", xxxxxxxx));
//接口地址
URL url = new URL("xxxxxxxxxxx");
//创建连接
HttpURLConnection connection = (HttpURLConnection) url .openConnection(proxy);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.addRequestProperty("Content-Type","application/json");
connection.addRequestProperty("Authorization","Basic RG9jdG9yWDoyMjIyMjI=");
connection.connect();
//将base64图片转为list参数形式
List<String> Image1=new ArrayList<>();
Image1.add("data:image/jpg;base64,"+leftImage);
List<String> Image2=new ArrayList<>();
Image2.add("data:image/jpg;base64,"+rightImage);
//POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject obj = new JSONObject();
//Map<String, String> map = new HashMap<String, String>();
//map.put("pic", image_data.toString());
//map.put("requestId", requestId);
JSONObject json = JSONObject.fromObject("{\"birthday\":\"2001-11-10\",\"gender\":\"m\"},\"name\":\"test1\"");
obj.element("patient", json);
obj.element("img_left_eye", Image1);
obj.element("img_right_eye", Image2);
//System.out.println("patientID:"+"P00"+PiCi+f);
//obj.putAll(map);
//out.writeBytes(obj.toString());
//System.out.println(obj.toString());
String str = obj.toString();
//System.out.println(str);
out.writeBytes(str);
//out.write(str.getBytes("utf-8"));
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
//响应码
//System.out.println(connection.getResponseCode());
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
//Https的认证(全部信任)
static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
//Https忽略证书
class NullHostNameVerifier implements HostnameVerifier {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}
}