使用Java程序通过http post访问ABAP Netweaver服务器_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用Java程序通过http post访问ABAP Netweaver服务器

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

 2020/3/30 12:32:23  JerryWang_SAP  程序员俱乐部  我要评论(0)
  • 摘要:#CreatedbyJerryWang,lastmodifiedonAug20,2014sourcecode如下:```Javaimportjava.io.FileNotFoundException;importjava.io.IOException;importorg.apache.commons.codec.binary.Base64;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg
  • 标签:程序 使用 net Java 服务器 服务 HTTP

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

source code如下:

```Java

class="language-text" style="margin: 0px; padding: 0px; border-radius: 0px; font-family: Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace; font-size: inherit; background-color: inherit;">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:

?

```xml

<a href=’http://www.aa.com’ target=’_blank’>American Airlines</a><br /><a href=’Air Canada - The Official Website’ 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=’英国航空 | 在线预订航班、假期、城市旅游和办理登机手续’ 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=’airberlin’ 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=’Fly with Australia’s Favourite Airline’ 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的解决方案: [Repost: No more 'unable to find valid certification path to requested target'](code.naishe.in/2011/07/)

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

上一篇: Java并发编程之set集合的线程安全类你知道吗 下一篇: 没有下一篇了!
发表评论
用户名: 匿名