使用Java程序通过http post访问ABAP Netweaver服务器
source code如下:
[Java] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import java.io.FileNotFoundException;
import java.io.IOException;
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.util.EntityUtils;
@SuppressWarnings("deprecation")
public class MyHTTPPost
{
final private static String POST_PREFIX = <replace with your own application server url>;
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.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);
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");
}
}
|
运行结果:
application server会解析出Java program传入的http 请求中的"a", 然后从DB table scarr 中查找对应的记录,返回给Java side:
[HTML] 纯文本查看 复制代码
|
1
|
<a href=’http://www.aa.com’ target=’_blank’>American Airlines</a><br /><a href=’http://www.aircanada.ca’ target=’_blank’>Air Canada</a><br /><a href=’http://www.airfrance.fr’ target=’_blank’>Air France</a><br /><a href=’http://www.alitalia.it’ target=’_blank’>Alitalia</a><br /><a href=’http://www.british-airways.com’ target=’_blank’>British Airways</a><br /><a href=’http://www.airpacific.com’ target=’_blank’>Air Pacific</a><br /><a href=’http://www.continental.com’ target=’_blank’>Continental Airlines</a><br /><a href=’http://www.delta-air.com’ target=’_blank’>Delta Airlines</a><br /><a href=’http://www.airberlin.de’ target=’_blank’>Air Berlin</a><br /><a href=’http://www.lufthansa.com’ target=’_blank’>Lufthansa</a><br /><a href=’http://www.laudaair.com’ target=’_blank’>Lauda Air</a><br /><a href=’http://www.jal.co.jp’ target=’_blank’>Japan Airlines</a><br /><a href=’http://www.nwa.com’ target=’_blank’>Northwest Airlines</a><br /><a href=’http://www.qantas.com.au’ target=’_blank’>Qantas Airways</a><br /><a href=’http://www.saa.co.za’ target=’_blank’>South African Air.</a><br /><a href=’http://www.singaporeair.com’ target=’_blank’>Singapore Airlines</a><br /><a href=’http://www.ual.com’ target=’_blank’>United Airlines</a>
|
摘自:https://yq.aliyun.com/articles/746780?spm=a2c4e.11157919.spm-cont-list.7.146cf204XrSXWY
本文介绍了一种使用Java程序通过HTTP POST方法访问ABAP Netweaver服务器的方法。源代码示例展示了如何设置HTTP客户端,创建POST请求,以及如何处理响应。此示例包括了设置请求头、构造多部分实体以及解析服务器返回的数据。
154

被折叠的 条评论
为什么被折叠?



