URL theURL = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection)theURL.openConnection();connection.setRequestProperty("","")//具体要设计什么属性?还有对要发送的文件要做什么处理?请大虾帮助哈.
谢谢先
HttpURLConnection connection = (HttpURLConnection)theURL.openConnection();connection.setRequestProperty("","")//具体要设计什么属性?还有对要发送的文件要做什么处理?请大虾帮助哈.
谢谢先
conn.setDoInput(true) ;
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
content=
"mobile="+URLEncoder.encode(MobileNo)+
"&msg="+URLEncoder.encode(ShortMsg)+
"&sname="+URLEncoder.encode("")+
"&user="+URLEncoder.encode(RegMobile)+
"&passwd="+URLEncoder.encode(RegPasswd); output=new DataOutputStream(conn.getOutputStream());
output.writeBytes(content);
output.flush();
output.close();
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");
要不要对先文件进行 MIME编码,在写到输出流中?
还是直接写到输出流中?
还有几个问题能不能解释下
问题1.如果文件是图片或其他格式的文件呢?需不需要处理?问题2.还有 boundary=---------------------------7d318fd10011 //分隔是不是随便定义的?
问题3.还有个疑问 就是我试了一下,没有设置这些属性也能发送文件 好像没有影响 能不能说下为什么?
我用ethereal抓数据报刊到的,Content-type我设置成application/x-www-form-urlencoded 也是分成多部分发送的。什么原因呀?
这些属性设置有什么用嘛?uc.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, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");
能解释下的话 实在太感谢了
gzpoplar(poplar) 还在不在??// 发送文件:
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("\r\n");
sb = sb.append("Content-Disposition: form-data; name=\"1\"; filename=\"1.txt\"\r\n");
sb = sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
// 设置HTTP头:
hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + ";boundary=" + BOUNDARY);
hc.setRequestProperty("Content-Length", String.valueOf(data.length + file.length + end_data.length));
// 输出:
output = hc.openOutputStream();
output.write(data);
output.write(file);
output.write(end_data);
// 读取服务器响应:
// TODO...网上有一段例子是这样的 是不是 还需要这样处理?
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.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, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache"); String strHeader = "";
strHeader += "-----------------------------7d318fd100112\r\n";
strHeader += "Content-Disposition: form-data; name=\"fUpload\"; filename=\"E:\\xmltemp.xml\"\r\n";
strHeader += "Content-Type: text/xml\n";
String strTail = "";
strTail += "-----------------------------7d318fd100112\r\n";
strTail += "Content-Disposition: form-data; name=\"cbUpload\"\r\n\n";
strTail += "upload\r\n";
strTail += "-----------------------------7d318fd100112--\r\n\n\n";
DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); dos.write(strHeader.getBytes());
dos.writeBytes("\n"); // 直接把string写入dos
//byte[] b = strXML.getBytes("UTF-8");
//dos.write(b, 0, b.length); // 通过xml文件把数据写入dos
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(postxmlfile);
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setLineWidth(0);
format.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(dos, format);
serializer.asDOMSerializer();
serializer.serialize(doc); dos.writeBytes("\n");
dos.write(strTail.getBytes());
dos.close();
不过这段代码 没有看懂
能不能解释下
// 通过xml文件把数据写入dos
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(postxmlfile);
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setLineWidth(0);
format.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(dos, format);
serializer.asDOMSerializer();
serializer.serialize(doc);
用到的类我都没有见过
麻烦了