1 18 19 package org.apache.jmeter.protocol.http.sampler; 20 21 import java.io.BufferedInputStream ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.PrintWriter ; 27 import java.io.UnsupportedEncodingException ; 28 import java.net.HttpURLConnection ; 29 import java.net.URLConnection ; 30 31 import org.apache.jmeter.config.Argument; 32 import org.apache.jmeter.testelement.property.PropertyIterator; 33 34 37 38 public class PostWriter 39 { 40 protected final static String BOUNDARY = 41 "---------------------------7d159c1302d0y0"; 42 private final static byte[] CRLF = { 0x0d, 0x0A }; 43 protected static final String encoding = "iso-8859-1"; 45 46 49 public void sendPostData(URLConnection connection, HTTPSampler sampler) 50 throws IOException 51 { 52 String filename = sampler.getFilename(); 54 if ((filename != null) && (filename.trim().length() > 0)) 55 { 56 OutputStream out = connection.getOutputStream(); 57 writeln(out, "--" + BOUNDARY); 60 PropertyIterator args = sampler.getArguments().iterator(); 61 while (args.hasNext()) 62 { 63 Argument arg = (Argument) args.next().getObjectValue(); 64 writeFormMultipartStyle( 65 out, 66 arg.getName(), 67 (String ) arg.getValue()); 68 writeln(out, "--" + BOUNDARY); 69 } 70 writeFileToURL( 71 out, 72 filename, 73 sampler.getFileField(), 74 getFileStream(filename), 75 sampler.getMimetype()); 76 77 writeln(out, "--" + BOUNDARY + "--"); 78 out.flush(); 79 out.close(); 80 } 81 82 else 84 { 85 String postData = sampler.getQueryString(); 86 PrintWriter out = new PrintWriter (connection.getOutputStream()); 87 out.print(postData); 88 out.flush(); 89 } 90 } 91 92 public void setHeaders(URLConnection connection, HTTPSampler sampler) 93 throws IOException 94 { 95 ((HttpURLConnection ) connection).setRequestMethod("POST"); 96 97 String filename = sampler.getFileField(); 99 if ((filename != null) && (filename.trim().length() > 0)) 100 { 101 connection.setRequestProperty( 102 "Content-Type", 103 "multipart/form-data; boundary=" + BOUNDARY); 104 connection.setDoOutput(true); 105 connection.setDoInput(true); 106 } 107 108 else 110 { 111 String postData = sampler.getQueryString(); 112 connection.setRequestProperty( 113 "Content-Length", 114 "" + postData.length()); 115 connection.setRequestProperty( 116 "Content-Type", 117 "application/x-www-form-urlencoded"); 118 connection.setDoOutput(true); 119 } 120 } 121 122 private InputStream getFileStream(String filename) throws IOException 123 { 124 return new BufferedInputStream (new FileInputStream (filename)); 125 } 126 127 153 154 157 private void writeFileToURL( 158 OutputStream out, 159 String filename, 160 String fieldname, 161 InputStream in, 162 String mimetype) 163 throws IOException 164 { 165 writeln( 166 out, 167 "Content-Disposition: form-data; name=\"" 168 + encode(fieldname) 169 + "\"; filename=\"" 170 + encode(filename) 171 + "\""); 172 writeln(out, "Content-Type: " + mimetype); 173 out.write(CRLF); 174 175 byte[] buf = new byte[1024]; 176 int read; 181 while ((read = in.read(buf)) > 0) 182 { 183 out.write(buf, 0, read); 184 } 185 out.write(CRLF); 186 in.close(); 187 } 188 189 192 private void writeFormMultipartStyle( 193 OutputStream out, 194 String name, 195 String value) 196 throws IOException 197 { 198 writeln(out, "Content-Disposition: form-data; name=\"" + name + "\""); 199 out.write(CRLF); 200 writeln(out, value); 201 } 202 203 private String encode(String value) 204 { 205 StringBuffer newValue = new StringBuffer (); 206 char[] chars = value.toCharArray(); 207 for (int i = 0; i < chars.length; i++) 208 { 209 if (chars[i] == '\\') 210 { 211 newValue.append("\\\\"); 212 } 213 else 214 { 215 newValue.append(chars[i]); 216 } 217 } 218 return newValue.toString(); 219 } 220 221 228 229 private void writeln(OutputStream out, String value) 230 throws UnsupportedEncodingException , IOException 231 { 232 out.write(value.getBytes(encoding)); 233 out.write(CRLF); 234 } 235 } 236 | Popular Tags |