2 u013537651 u013537651 于 2014.10.23 11:13 提问

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
feiyun0112   Rxr 2014.10.23 12:18
已采纳

看看HttpWebRequest/HttpWebResponse

u013537651
u013537651 看了好几次看不懂
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!