陈小新 2014-10-23 03:13 采纳率: 100%
浏览 2151
已采纳

java 代码转成c#代码 求大神帮忙 谢谢

public static WeixinMedia uploadMedia(String accessToken, String type, String mediaFileUrl) {

WeixinMedia weixinMedia = null;

// 拼装请求地址

String uploadMediaUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";

uploadMediaUrl = uploadMediaUrl.replace("ACCESS_TOKEN", accessToken).replace("TYPE", type);

// 定义数据分隔符  
String boundary = "------------7da2e536604c8";  
try {  
    URL uploadUrl = new URL(uploadMediaUrl);  
    HttpURLConnection uploadConn = (HttpURLConnection) uploadUrl.openConnection();  
    uploadConn.setDoOutput(true);  
    uploadConn.setDoInput(true);  
    uploadConn.setRequestMethod("POST");  
    // 设置请求头Content-Type  
    uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);  
    // 获取媒体文件上传的输出流(往微信服务器写数据)  
    OutputStream outputStream = uploadConn.getOutputStream();  

    URL mediaUrl = new URL(mediaFileUrl);  
    HttpURLConnection meidaConn = (HttpURLConnection) mediaUrl.openConnection();  
    meidaConn.setDoOutput(true);  
    meidaConn.setRequestMethod("GET");  

    // 从请求头中获取内容类型  
    String contentType = meidaConn.getHeaderField("Content-Type");  
    // 根据内容类型判断文件扩展名  
    String fileExt = WeixinUtil.getFileEndWitsh(contentType);  
    // 请求体开始  
    outputStream.write(("--" + boundary + "\r\n").getBytes());  
    outputStream.write(String.format("Content-Disposition: form-data; name=\"media\"; filename=\"file1%s\"\r\n", fileExt).getBytes());  
    outputStream.write(String.format("Content-Type: %s\r\n\r\n", contentType).getBytes());  

    // 获取媒体文件的输入流(读取文件)  
    BufferedInputStream bis = new BufferedInputStream(meidaConn.getInputStream());  
    byte[] buf = new byte[8096];  
    int size = 0;  
    while ((size = bis.read(buf)) != -1) {  
        // 将媒体文件写到输出流(往微信服务器写数据)  
        outputStream.write(buf, 0, size);  
    }  
    // 请求体结束  
    outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());  
    outputStream.close();  
    bis.close();  
    meidaConn.disconnect();  

    // 获取媒体文件上传的输入流(从微信服务器读数据)  
    InputStream inputStream = uploadConn.getInputStream();  
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
    StringBuffer buffer = new StringBuffer();  
    String str = null;  
    while ((str = bufferedReader.readLine()) != null) {  
        buffer.append(str);  
    }  
    bufferedReader.close();  
    inputStreamReader.close();  
    // 释放资源  
    inputStream.close();  
    inputStream = null;  
    uploadConn.disconnect();  

    // 使用JSON-lib解析返回结果  
    JSONObject jsonObject = JSONObject.fromObject(buffer.toString());  
    // 测试打印结果  
    System.out.println("打印测试结果"+jsonObject);  
    weixinMedia = new WeixinMedia();  
    weixinMedia.setType(jsonObject.getString("type"));  
    // type等于 缩略图(thumb) 时的返回结果和其它类型不一样  
    if ("thumb".equals(type))  
        weixinMedia.setMediaId(jsonObject.getString("thumb_media_id"));  
    else  
        weixinMedia.setMediaId(jsonObject.getString("media_id"));  
        weixinMedia.setCreatedAt(jsonObject.getInt("created_at"));  
} catch (Exception e) {  
    weixinMedia = null;  
    String error = String.format("上传媒体文件失败:%s", e);  
    System.out.println(error);  
}  
return weixinMedia;  

}

转换成c#代码

  • 写回答

1条回答

  • feiyun0112 2014-10-23 04:18
    关注

    看看HttpWebRequest/HttpWebResponse

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型