import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
@SuppressWarnings("deprecation")
public class MyHTTPPost
{
final private static String POST_PREFIX = "https://ag3.wdf.sap.corp:44354/sap/crm/file_upload?query=";
private static String getRequestURL(String str)
{
String url = POST_PREFIX + str;
url = url + "&sid=" + Math.random();
return url;
}
private static void sendHTTPPostRequest(String str) throws FileNotFoundException
{
HttpClient httpclient = new DefaultHttpClient();
String raw = "WANGJER:Saptest1";
String encoded = Base64.encodeBase64String(raw.getBytes());
HttpPost httppost = new HttpPost(getRequestURL(str));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(Charset.forName(HTTP.UTF_8));
builder.addTextBody("firstName", "Jerry");
builder.addTextBody("lastName", "Wang");
try {
httppost.addHeader("Content-Type","multipart/form-data; boundary=assdsfdffafasf");
httppost.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
httppost.addHeader("Authorization", "Basic " + encoded);
HttpEntity httpEntity = builder.build();
httppost.setEntity(httpEntity);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
String response = EntityUtils.toString(resEntity);
System.out.println(response);
}
catch (IOException e)
{
System.out.println(e.getLocalizedMessage());
e.printStackTrace();
System.out.println("error!");
}
}
public static void main(String[] args) throws ClientProtocolException, IOException
{
sendHTTPPostRequest("a");
}
}
```要获取更多Jerry的原创文章,请关注公众号"汪子熙":
<img src="https://user-images.githubusercontent.com/5669954/61616089-2a87e180-ac9a-11e9-861d-c29c2cf897af.png">