使用简单的Java代码在SAP C4C里创建销售订单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用简单的Java代码在SAP C4C里创建销售订单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3365字,纯文字阅读大概需要5分钟。
内容图文
![使用简单的Java代码在SAP C4C里创建销售订单](/upload/InfoBanner/zyjiaocheng/843/25ceb5ecaa2c4ec4833753a7bb160b88.jpg)
需要创建的销售订单的明细通过硬编码指定:
比如销售订单的描述为Jerry Test 2019-1-23 16:05PM
执行之后,看到Status Code 201,说明创建成功:
到UI上能看到成功创建的销售订单:
实现代码:
package odata;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class SimpleOrderCreator {
private final String SERVICEURL = "https://my5000jerry.c4c.saphybriscloud.cn/sap/c4c/odata/cust/v1/zjerrysalesorder/CustomerQuoteCollection";
HttpClient m_httpClient;
private HttpClient getHttpClient() {
if (this.m_httpClient == null) {
this.m_httpClient = HttpClientBuilder.create().build();
}
return this.m_httpClient;
}
private String getCSRFToken(){
final HttpGet get = new HttpGet(SERVICEURL);
get.setHeader("Authorization", "Basic 0FOR0pFUlJZMTpTYXB0ZXN0MQ==");
get.setHeader("x-csrf-token", "fetch");
HttpHost proxy = new HttpHost("proxy.sha.sap.corp", 8080, "http");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
get.setConfig(config);
HttpResponse response;
String token = null;
try {
response = getHttpClient().execute(get);
/*Header[] header = response.getAllHeaders();
for( int i =0; i < header.length; i++){
System.out.println("Header: " + header[i].getValue());
}*/
token = response.getFirstHeader("x-csrf-token").getValue();
System.out.println("token: " + token);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException | UnsupportedOperationException e) {
e.printStackTrace();
}
return token;
}
private void createSO(String token,String body) {
final HttpPost post = new HttpPost(
URI.create(SERVICEURL));
post.setHeader("Authorization",
"Basic 0FOR0pFUlJZMTpTYXB0ZXN0MQ==");
post.setHeader("Content-Type", "application/json");
post.setHeader("X-CSRF-Token", token);
HttpEntity entity = null;
try {
entity = new StringEntity(body);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
post.setEntity(entity);
HttpResponse response = null;
try {
response = getHttpClient().execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Response statusCode for Batch => "
+ response.getStatusLine().getStatusCode());
}
public static void main(String[] args) {
SimpleOrderCreator tool = new SimpleOrderCreator();
String token = tool.getCSRFToken();
String body = "{" +
"\"Name\": \"Jerry Test 2019-1-23 16:05PM\"," +
"\"TypeCode\": \"2059\"," +
"\"BuyerID\":\"ee\"," +
"\"PartyID\":\"60102\", " +
"\"CustomerQuoteText\":[{" +
"\"Text\": \"test comment by Jerry Wang\", " +
"\"TypeCode\": \"10024\"" +
"}]," +
"\"CustomerQuoteItem\":[{ " +
"\"ProductID\": \"1042416\"" +
"}]," +
"\"CustomerQuoteItemProposal\":[{" +
"\"ProductUUID\": \"00163E72-09C6-1EE8-BBDC-AC5F0CB0D795\"," +
"\"Quantity\": \"1\"," +
"\"unitCode\": \"EA\"" +
"}]" +
"}";
tool.createSO(token, body);
}
}
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
内容总结
以上是互联网集市为您收集整理的使用简单的Java代码在SAP C4C里创建销售订单全部内容,希望文章能够帮你解决使用简单的Java代码在SAP C4C里创建销售订单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。