浮生若梦z 2015-09-27 02:17 采纳率: 0%
浏览 1488

微信上传图片,非公众号

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

}

返回的状态是失败,大哥们帮忙看下,感激不尽

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-27 19:56
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置