首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

java语言用vb.net兑现

2012-10-29 
java语言用vb.net实现以下是java代码,特别是sendData怎么用vb实现?—————————————————————————————————————

java语言用vb.net实现
以下是java代码,特别是sendData怎么用vb实现?
————————————————————————————————————————————
private final String SERVICE_URL = "http://test.m.com.cn";
/**
* 返回数据的类型
*/
private final String DATA_TYPE = "json";

/**
* 用户名
*/
private final String USER = "ze_tran"; //superuser

/**
* 密码
*/
private final String PWD = "14452"; //cltr112233

/**
* 获取token
*  
*/
public String getToken() throws IOException
{
String sUrl = SERVICE_URL + "/docking/auto/gettoken.shtml?loginid=" + USER + "&pwd=" + PWD + "&datatype=" + DATA_TYPE;
System.out.println("send getToken request url:" + sUrl);
String response = "{}";
try
{
MyProcURLConnection mpuc = new MyProcURLConnection();
response = JustHttp.doGetStr(sUrl, 3 * 60000, mpuc, "UTF-8");
System.out.println(mpuc.resProps.get("Set-Cookie"));
// comdzy.strUtil.getMPartStr(oriStr, bMark, eMark)
// JSESSIONID=0AC8F36F61DF91F1AA71878F5B9DAA48;
}
catch (Exception e)
{
e.printStackTrace();
}
JSONObject obj = JSONObject.fromObject(response);
String token = obj.getJSONObject("result").getString("token");
return token;
}

/**
* 发送数据  
* 本方法不使用justHttp的原因:因为这是给用户的Demo,用户并没有JustHttp
*/
public void sendData(String token)
{
try
{
//定义数据的年份、月份、中心编号、数据地址
//请在使用时更改这些值  
String year = "2012";
String month = "03";
String orgid = "14341";
String data_url = "D:/2012_3_14343.zip";
data_url = "v:/a111.zip";
// data_url="d:/share/HQMS/数据对接/guoshiping1.zip";
// data_url="v:/hqcmtrandemo.zip";
String sUrl = SERVICE_URL + "/autoReqTran.shtml?year=" + year + "&month=" + month + "&orgid=" + orgid + "&token=" + token + "&datatype=" + DATA_TYPE;
// sUrl = SERVICE_URL+"/usp/doInnerAction?Action=autoreqFile&year="+year+"&month="+month+"&orgid="+orgid+"&token="+token+"&datatype="+DATA_TYPE;
System.out.println("senddata url=" + sUrl);
URL url = new URL(sUrl);
// URL url = new URL(SERVICE_URL+"/usp/doInnerAction?Action=autoreqFile&year="+year+"&month="+month+"&orgid="+orgid+"&token="+token+"&datatype="+DATA_TYPE);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

//设置连接超时时长,根据数据量大小而定
connection.setConnectTimeout(10 * 60 * 1000);

//设置读取超时
connection.setReadTimeout(50000);

//设置允许输出
connection.setDoOutput(true);

//设置允许输入
connection.setDoInput(true);

//设置不缓存
connection.setUseCaches(false);

//设置为POST方式,必须的
connection.setRequestMethod("POST");

//读取本地ZIP文件
ByteArrayOutputStream bout = new ByteArrayOutputStream();
File file = new File(data_url);
InputStream is = new FileInputStream(file);
IOUtils.copy(is, bout);

//----------设置连接的参数---------------

//设置维持长连接
connection.setRequestProperty("Connection", "Keep-Alive");

//设置字符编码集
connection.setRequestProperty("Charset", "UTF-8");

//设置长度
connection.setRequestProperty("Content-Length", String.valueOf(bout.toByteArray().length));

//设置内容为流
connection.setRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=Bounday---------------------------7d318fd100112");



//设置不缓存
connection.setRequestProperty("Cache-Control", "no-cache");

//不允许服务器以压缩的格式返回结果:因为是需要本地解压的
// connection.setRequestProperty("Accept-Encoding", "gzip, deflate");

//将文件写入输出流
OutputStream os = connection.getOutputStream();

os.write(bout.toByteArray());
bout.close();
os.close();
is.close();

InputStream in = connection.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (int b = in.read(); b >= 0; b = in.read())
{
outputStream.write((byte)b);
}
String res = new String(outputStream.toByteArray(), "UTF-8").trim();
outputStream.close();
in.close();
System.out.println("sendData return: " + res);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}

[解决办法]
详细去了解httpwebreqeust

[解决办法]
撇開代碼轉換,直接搜索你想要實現的功能(C#),還會快些.

热点排行