使用Java程序通过http post访问ABAP Netweaver服务器

简介:

Created by Jerry Wang, last modified on Aug 20, 2014

source code如下:

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:

<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>

如果遇到server certificate问题,可以参考这个link的解决方案: http://code.naishe.in/2011/07/looks-like-article-no-more-unable-to.html

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
10天前
|
Java
java原生发送http请求
java原生发送http请求
|
1月前
|
JSON 数据格式
第三方系统或者工具通过 HTTP 请求发送给 ABAP 系统的数据,应该如何解析试读版
第三方系统或者工具通过 HTTP 请求发送给 ABAP 系统的数据,应该如何解析试读版
26 0
|
2天前
|
网络协议 Java API
深度剖析:Java网络编程中的TCP/IP与HTTP协议实践
【4月更文挑战第17天】Java网络编程重在TCP/IP和HTTP协议的应用。TCP提供可靠数据传输,通过Socket和ServerSocket实现;HTTP用于Web服务,常借助HttpURLConnection或Apache HttpClient。两者结合,构成网络服务基础。Java有多种高级API和框架(如Netty、Spring Boot)简化开发,助力高效、高并发的网络通信。
|
7天前
|
域名解析 网络协议 应用服务中间件
阿里云服务器配置免费https服务
阿里云服务器配置免费https服务
|
29天前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
20 0
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
108 1
|
1月前
|
网络安全 Apache PHP
Liunx服务器如何配置https(二)
Liunx服务器如何配置https(二)
33 0
Liunx服务器如何配置https(二)
|
1月前
|
Apache
Liunx服务器如何配置https(一)
Liunx服务器如何配置https(一)
32 0
Liunx服务器如何配置https(一)
|
1月前
|
弹性计算 网络安全 Apache
windows server2012服务器下PHPstudy配置ssl证书(https配置)
windows server2012服务器下PHPstudy配置ssl证书(https配置)
66 0

热门文章

最新文章