}
else {
newUrlBuffer.append("&");
}
for(int i = 0; i < formInputs.length; i++) {
FormInput parameter = formInputs[i];
String name = parameter.getName();
String encName = URLEncoder.encode(name, "UTF-8");
if(parameter.isText()) {
if(firstParam) {
firstParam = false;
}
else {
newUrlBuffer.append("&");
}
String valueStr = parameter.getTextValue();
String encValue = URLEncoder.encode(valueStr, "UTF-8");
newUrlBuffer.append(encName);
newUrlBuffer.append("=");
newUrlBuffer.append(encValue);
}
}
resolvedURL = new java.net.URL(newUrlBuffer.toString());
}
else {
resolvedURL = action;
}
URL urlForLoading;
if(resolvedURL.getProtocol().equals("file")) {
// Remove query so it works.
try {
String ref = action.getRef();
String refText = ref == null || ref.length() == 0 ? "" : "#" + ref;
urlForLoading = new URL(resolvedURL.getProtocol(), action.getHost(), action.getPort(), action.getPath() + refText);
} catch(java.net.MalformedURLException throwable) {
urlForLoading = action;
}
}
else {
urlForLoading = resolvedURL;
}
// Using potentially different URL for loading.
boolean isPost = "POST".equals(actualMethod);
URLConnection connection = urlForLoading.openConnection();
connection.setRequestProperty("User-Agent", getUserAgentContext().getUserAgent());
connection.setRequestProperty("Cookie", "");
HttpURLConnection hc = null;
if (connection instanceof HttpURLConnection) {
hc = (HttpURLConnection) connection;
hc.setRequestMethod(actualMethod);
// Do not follow redirects
hc.setInstanceFollowRedirects(false);
}
if(isPost) {
connection.setDoOutput(true);
ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
boolean firstParam = true;
if(formInputs != null) {
for(int i = 0; i < formInputs.length; i++) {
FormInput parameter = formInputs[i];
String name = parameter.getName();
String encName = URLEncoder.encode(name, "UTF-8");
if(parameter.isText()) {
if(firstParam) {
firstParam = false;
}
else {
bufOut.write((byte) '&');
}
String valueStr = parameter.getTextValue();
String encValue = URLEncoder.encode(valueStr, "UTF-8");
bufOut.write(encName.getBytes("UTF-8"));
bufOut.write((byte) '=');
bufOut.write(encValue.getBytes("UTF-8"));
}