Skip to content

Instantly share code, notes, and snippets.

@vparihar01
Last active December 15, 2015 15:49
Show Gist options
  • Save vparihar01/5284708 to your computer and use it in GitHub Desktop.
Save vparihar01/5284708 to your computer and use it in GitHub Desktop.
public String demoMethod(List<NameValuePair> params) {
String args = params.get(1).toString();
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "goBonziteam";
/*int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
File temp_file = new File("foo.txt");*/
HttpsURLConnection conn;
String response= "";
// open a URL connection
try {
URL url = new URL(webServiceUrl);
// Open a HTTP connection to the URL
conn = (HttpsURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
conn.setChunkedStreamingMode(0);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cookie","PHP_SSL_TS=1kngj574m9kdank1b3hgt3u8n6; path=/");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
//conn.setRequestProperty("Transfer-Encoding","chunked");
DataOutputStream dos;
dos = new DataOutputStream( conn.getOutputStream() );
// Send parameter #1
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"cmd\"" + lineEnd + lineEnd);
System.out.println("Content-Disposition: form-data; name=\"cmd\"" + lineEnd + lineEnd);
dos.writeBytes("tsql" + lineEnd);
// Send parameter #2
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"args\"" + lineEnd + lineEnd);
System.out.println("Content-Disposition: form-data; name=\"args\"" + lineEnd + lineEnd);
dos.writeBytes(args + lineEnd);
System.out.println("args======"+args);
System.out.println("dos======"+dos.toString());
/*// Send a binary file
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + temp_file.getName() +"\"" + lineEnd);
dos.writeBytes(lineEnd);
*/
// create a buffer of maximum size
/*
FileInputStream fileInputStream = new ;
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
} */
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
//fileInputStream.close();
dos.flush();
dos.close();
Scanner inStream = new Scanner(conn.getInputStream());
while(inStream.hasNextLine())
response+=(inStream.nextLine());
System.out.println(response);
/* PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.close();*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment