首页 新闻 会员 周边

这个微信视频上传的代码可以改成C#的吗?

0
悬赏园豆:10 [已解决问题] 解决于 2017-05-10 10:06

public static String postFile(String url, String filePath,
String title,String introduction) {
File file = new File(filePath);
if(!file.exists())
return null;
String result = null;
try {
URL url1 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
String boundary = "-----------------------------"+System.currentTimeMillis();
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);

OutputStream output = conn.getOutputStream();
output.write(("--" + boundary + "\r\n").getBytes());
output.write(String.format("Content-Disposition: form-data; name=\"media\"; filename=\"%s\"\r\n", file.getName()).getBytes());
output.write("Content-Type: video/mp4 \r\n\r\n".getBytes());
byte[] data = new byte[1024];
int len =0;
FileInputStream input = new FileInputStream(file);
while((len=input.read(data))>-1){
output.write(data, 0, len);
}
output.write(("--" + boundary + "\r\n").getBytes());
output.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n".getBytes());
output.write(String.format("{\"title\":\"%s\", \"introduction\":\"%s\"}",title,introduction).getBytes());
output.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes());
output.flush();
output.close();
input.close();
InputStream resp = conn.getInputStream();
StringBuffer sb = new StringBuffer();
while((len= resp.read(data))>-1)
sb.append(new String(data,0,len,"utf-8"));
resp.close();
result = sb.toString();
System.out.println(result);
} catch (ClientProtocolException e) {
logger.error("postFile,不支持http协议",e);
} catch (IOException e) {
logger.error("postFile数据传输失败",e);
}
logger.info("{}: result={}",url,result);
return result;
}

笑叹、的主页 笑叹、 | 初学一级 | 园豆:34
提问于:2016-09-07 09:08
< >
分享
最佳答案
0

Java确实跟C#很像

看着改边搜索边改~这样能补充好多基础知识

收获园豆:10
远扬 | 初学一级 |园豆:30 | 2016-09-07 10:05

号的

笑叹、 | 园豆:34 (初学一级) | 2016-09-07 11:00
其他回答(1)
0

 肯定是可以的吧。

顾晓北 | 园豆:10844 (专家六级) | 2016-09-07 09:12

大神,求教

支持(0) 反对(0) 笑叹、 | 园豆:34 (初学一级) | 2016-09-07 09:13

@SummerTang: Java跟C#差不多,看着改不就行了吗?

支持(0) 反对(0) 顾晓北 | 园豆:10844 (专家六级) | 2016-09-07 09:15

@SummerTang: 不能,不提供代码,C#跟Java是很像的,你之所以觉得差的多是因为你基础知识欠缺。。。

支持(0) 反对(0) 顾晓北 | 园豆:10844 (专家六级) | 2016-09-07 09:51

@顾晓北: 好的,我自己琢磨去

支持(0) 反对(0) 笑叹、 | 园豆:34 (初学一级) | 2016-09-07 11:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册