package com.httpclient.cn;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class SendImg {
public static String requestUrl = "https://file2.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json";
public static void main(String[] args) {
sendImg(requestUrl,null);
}
public static String sendImg(String requestUrl,String cookie){
System.setProperty ("jsse.enableSNIExtension", "false");
try {
File file = new File("C:/Users/JimmyHen/Desktop/20150926101632.jpg");
String clientMediaId = Long.toString(System.currentTimeMillis());
URL url = new URL(requestUrl);
String boundary = "-----------------------------140683023824016";
String newLine = "\r\n";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(5000);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
//设置请求头信息
connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ");
connection.setRequestProperty("Accept-Encoding","gzip, deflate");
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Content-Length",Long.toString(file.length()));
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
connection.setRequestProperty("host","file2.wx.qq.com");
connection.setRequestProperty("Origin","https://wx2.qq.com");
connection.setRequestProperty("Referer","https://wx2.qq.com/?&lang=zh_CN");
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0");
OutputStream outputStream = new DataOutputStream(connection.getOutputStream()); // utf-8编码
//构建请求正文
StringBuffer sb = new StringBuffer();
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"id\"");
sb.append(newLine).append(newLine);
sb.append("WU_FILE_4").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"name\"");
sb.append(newLine).append(newLine);
sb.append(file.getName()).append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"type\"");
sb.append(newLine).append(newLine);
sb.append("image/jpeg").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"lastModifiedDate\"");
sb.append(newLine).append(newLine);
sb.append("Sat Sep 26 2015 10:16:39 GMT+0800").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"mediatype\"");
sb.append(newLine).append(newLine);
sb.append("pic").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"uploadmediarequest\"");
sb.append(newLine).append(newLine);
sb.append("{\"BaseRequest\":{\"Uin\":699244040,\"Sid\":\"/p3w2f48GTHbJCFn\",\"Skey\":\"@crypt_c799f4fc_ebd218359c19edb6aabd6890e529fb66\"");
sb.append(",\"DeviceID\":\"e206799846381690\"},\"ClientMediaId\":\""+clientMediaId+"\",\"TotalLen\":\""+file.length()+"\",\"StartPos\":0,\"DataLen\"");
sb.append(":\""+file.length()+"\",\"MediaType\":4}\")").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"webwx_data_ticket\"");
sb.append(newLine).append(newLine);
sb.append("AQa9tWY2oKP6S6XO7XaP0JZz").append(newLine);
sb.append(boundary).append(newLine);
sb.append("Content-Disposition: form-data; name=\"pass_ticket\"");
sb.append(newLine).append(newLine);
sb.append("HzNuoS4USp/BzlmUCemcNN+8bu65jR1gKlf/JZR/PUt3xbyyzV4iWRaH1G+Sdznb").append(newLine);
sb.append("Content-Disposition: form-data; name=\"filename\"; filename=\"20150926101632.jpg\"").append(newLine);
sb.append("Content-Type: image/jpeg");
sb.append(newLine).append(newLine);
outputStream.write(sb.toString().getBytes());//写入Post参数信息
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] byteOut = new byte[1024];
while((bytes = dis.read(byteOut))!= -1){
outputStream.write(byteOut);//写照片
}
outputStream.write(newLine.getBytes());
dis.close();
byte[] endFile = (newLine + boundary+"--"+newLine).getBytes();
outputStream.write(endFile);
outputStream.flush();
outputStream.close();
InputStream inputStream = connection.getInputStream();
StringBuffer result = new StringBuffer();
byte[] tmp = new byte[1024];
int l;
while((l = inputStream.read(tmp)) != -1){
result.append(new String(tmp,0,l));
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
String result = null;
return result;
}
}
class JEEWeiXinX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
返回的状态是失败,大哥们帮忙看下,感激不尽