/**
*
*/
package org.qd.sp;
/**
* @author mpoli_fujitsu
*
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpClientExample http = new HttpClientExample();
/*System.out.println("Testing 1 - Send Http GET request");
http.sendGet();*/
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "http://qdhqspdev09:9999/Api/SPFileOp/testofservice";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "http://qdhqspdev09:9999/api/SPFileOp/UploadFile?filepath=/Departments/IT/finance&filename=abc.pdf";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
byte[] fileContent = getBytes();
// add header
//post.setHeader("User-Agent", USER_AGENT);
MultipartEntity entity = new MultipartEntity();
entity.addPart("filepath",new StringBody("/Departments/Finance/"));
entity.addPart("file", new ByteArrayBody(fileContent, "MyFirstTest.pdf"));
post.setEntity(entity);
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
String json = EntityUtils.toString(httpResponse.getEntity());
/*List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("filepath", "/Departments/IT/"));
urlParameters.add(new BasicNameValuePair("filename", "test.pdf"));
urlParameters.add(new BasicNameValuePair("api", "XMLInput"));
urlParameters.add(new BasicNameValuePair("caller", ""));
urlParameters.add(new BasicNameValuePair("num", "12345"));*/
//post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
public byte[] getBytes()
{
java.io.File file = new java.io.File("C:/Users/mpoli_fujitsu/Desktop/Test (2).pdf");
System.out.println(file.getName());
byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file.toString());
fileInputStream.read(b);
for (int i = 0; i < b.length; i++) {
// System.out.print((char)b[i]);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
return b;
}
}
*
*/
package org.qd.sp;
/**
* @author mpoli_fujitsu
*
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpClientExample http = new HttpClientExample();
/*System.out.println("Testing 1 - Send Http GET request");
http.sendGet();*/
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "http://qdhqspdev09:9999/Api/SPFileOp/testofservice";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "http://qdhqspdev09:9999/api/SPFileOp/UploadFile?filepath=/Departments/IT/finance&filename=abc.pdf";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
byte[] fileContent = getBytes();
// add header
//post.setHeader("User-Agent", USER_AGENT);
MultipartEntity entity = new MultipartEntity();
entity.addPart("filepath",new StringBody("/Departments/Finance/"));
entity.addPart("file", new ByteArrayBody(fileContent, "MyFirstTest.pdf"));
post.setEntity(entity);
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
String json = EntityUtils.toString(httpResponse.getEntity());
/*List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("filepath", "/Departments/IT/"));
urlParameters.add(new BasicNameValuePair("filename", "test.pdf"));
urlParameters.add(new BasicNameValuePair("api", "XMLInput"));
urlParameters.add(new BasicNameValuePair("caller", ""));
urlParameters.add(new BasicNameValuePair("num", "12345"));*/
//post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
public byte[] getBytes()
{
java.io.File file = new java.io.File("C:/Users/mpoli_fujitsu/Desktop/Test (2).pdf");
System.out.println(file.getName());
byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file.toString());
fileInputStream.read(b);
for (int i = 0; i < b.length; i++) {
// System.out.print((char)b[i]);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
return b;
}
}
No comments:
Post a Comment