1. C# POST 请求示例
using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ns_sample_post
{
class sample_post
{
private static string url = "https://api-gateway.coupang.com";
private static string schema = "https";
private static int port = 443;
private string path = "/v2/providers/openapi/apis/api/v4/vendors/A000*****/outboundShippingCenters";
//replace with your own accessKey
private static string accessKey = "****";
//replace with your own secretKey
private static string secretKey = "****";
public void test()
{
string method = "POST";
//replace with your own shipping location json data
String strjson = "";
strjson = strjson + "{" + "\r\n";
strjson = strjson + " \"vendorId\": \"A000*****\"," + "\r\n";
strjson = strjson + " \"userId\": \"******\"," + "\r\n";
strjson = strjson + " \"shippingPlaceName\": \"TEST0001\"," + "\r\n";
strjson = strjson + " \"placeAddresses\": [" + "\r\n";
strjson = strjson + " {" + "\r\n";
strjson = strjson + " \"addressType\": \"JIBUN\"," + "\r\n";
strjson = strjson + " \"countryCode\": \"KR\"," + "\r\n";
strjson = strjson + " \"companyContactNumber\": \"02-111-1111\"," + "\r\n";
strjson = strjson + " \"phoneNumber2\": \"02-111-1111\"," + "\r\n";
strjson = strjson + " \"returnZipCode\": \"32711\"," + "\r\n";
strjson = strjson + " \"returnAddress\": \"충청남도 금산군 추부면 추풍로\"," + "\r\n";
strjson = strjson + " \"returnAddressDetail\": \"111-11\"" + "\r\n";
strjson = strjson + " }" + "\r\n";
strjson = strjson + " ]," + "\r\n";
strjson = strjson + " \"remoteInfos\": [" + "\r\n";
strjson = strjson + " {" + "\r\n";
strjson = strjson + " \"deliveryCode\": \"HYUNDAI\"," + "\r\n";
strjson = strjson + " \"jeju\": 5000," + "\r\n";
strjson = strjson + " \"notJeju\": 2500" + "\r\n";
strjson = strjson + " }" + "\r\n";
strjson = strjson + " ]" + "\r\n";
strjson = strjson + "}" + "\r\n";
var uriBuilder = new UriBuilder(url + path);
uriBuilder.Scheme = schema;
uriBuilder.Port = port;
Uri finalUrl = uriBuilder.Uri;
Console.WriteLine(strjson);
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUrl.ToString());
request.Timeout = 10000;
request.Method = method;
byte[] data = Encoding.UTF8.GetBytes(strjson);
request.ContentType = "application/json;charset=UTF-8";
request.Headers["Authorization"] = GetHmac(method, "");
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
var response = (HttpWebResponse)request.GetResponse();
// Display the status ...
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Response Status Code is OK and StatusDescription is: {0}", response.StatusDescription);
var responseStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader reader = new StreamReader(responseStream, encode);
string responseString = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(String.Format("Response: {0}", responseString));
}
else
{
Console.WriteLine("Response Status Code is Not OK and StatusDescription is: {0}", response.StatusDescription);
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
using (Stream data = e.Response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
catch (Exception e)
{
Console.WriteLine("The following Exception was raised : {0}", e.Message);
}
}
private string GetHmac(string method, string query)
{
string algorithm = "HmacSHA256";
string datetime = DateTime.Now.ToUniversalTime().ToString("yyMMddTHHmmssZ");
string message = GenerateFormattedMessage(datetime, method, query);
string signature = CreateTokenByHMACSHA256(message);
return GenerateFormattedHeader(algorithm, datetime, signature);
}
private string GenerateFormattedMessage(string datetime, string method, string query)
{
return String.Format("{0}{1}{2}{3}", datetime, method, path, query);
}
private String GenerateFormattedHeader(string algorithm, string datetime, string signature)
{
return String.Format("CEA algorithm={0}, access-key={1}, signed-date={2}, signature={3}", algorithm, accessKey, datetime, signature);
}
private String CreateTokenByHMACSHA256(string message)
{
secretKey = secretKey ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secretKey);
var hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
return ByteToString(hmacsha256.ComputeHash(messageBytes));
}
public string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("x2"); // hex format
}
return sbinary;
}
static void Main(string[] args)
{
sample_post myobj = new sample_post();
myobj.test();
}
}
}
2. C# GET 请求示例
using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ns_sample_get
{
class sample_get
{
private static string url = "https://api-gateway.coupang.com";
private static string schema = "https";
private static int port = 443;
//replace with your own vendorId
private string path="/v2/providers/openapi/apis/api/v4/vendors/A00******/returnRequests";
//replace with your own accessKey
private static string accessKey = "****";
//replace with your own secretKey
private static string secretKey = "****";
public void test(){
string method="GET";
var uriBuilder = new UriBuilder(url+path);
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["createdAtFrom"] = "2018-08-09";
parameters["createdAtTo"] = "2018-08-09";
parameters["status"] = "UC";
uriBuilder.Query = parameters.ToString();
string query=uriBuilder.Query.ToString().Remove(0,1);
uriBuilder.Scheme = schema;
uriBuilder.Port = port;
Uri finalUrl = uriBuilder.Uri;
try{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUrl.ToString());
request.Timeout = 10000;
request.Method = method;
request.ContentType = "application/json;charset=UTF-8";
request.Headers["Authorization"] = GetHmac(method,query);
var response = (HttpWebResponse)request.GetResponse();
// Display the status ...
if (response.StatusCode == HttpStatusCode.OK){
Console.WriteLine("Response Status Code is OK and StatusDescription is: {0}",response.StatusDescription);
var responseStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader reader = new StreamReader( responseStream, encode );
string responseString = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(responseString);
//Console.WriteLine(String.Format("Response: {0}", responseString));
}else{
Console.WriteLine("Response Status Code is Not OK and StatusDescription is: {0}",response.StatusDescription);
}
}
catch(WebException e)
{
Console.WriteLine("WebException Raised. The following error occured : {0}",e.Status);
Console.WriteLine(e.Message);
}
catch(Exception e)
{
Console.WriteLine("The following Exception was raised : {0}",e.Message);
}
}
private string GetHmac(string method,string query)
{
string algorithm = "HmacSHA256";
string datetime = DateTime.Now.ToUniversalTime().ToString("yyMMddTHHmmssZ");
string message = GenerateFormattedMessage(datetime, method, query);
string signature = CreateTokenByHMACSHA256(message);
return GenerateFormattedHeader(algorithm, datetime, signature);
}
private string GenerateFormattedMessage(string datetime, string method, string query)
{
return String.Format("{0}{1}{2}{3}", datetime, method, path, query);
}
private String GenerateFormattedHeader(string algorithm, string datetime, string signature)
{
return String.Format("CEA algorithm={0}, access-key={1}, signed-date={2}, signature={3}", algorithm, accessKey, datetime, signature);
}
private String CreateTokenByHMACSHA256(string message)
{
secretKey = secretKey ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secretKey);
var hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
return ByteToString(hmacsha256.ComputeHash(messageBytes));
}
public string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("x2"); // hex format
}
return sbinary;
}
static void Main(string[] args)
{
sample_get myobj= new sample_get();
myobj.test();
}
}
}
3. C# PUT 请求示例
using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ns_sample_put
{
class sample_put
{
private static string url = "https://api-gateway.coupang.com";
private static string schema = "https";
private static int port = 443;
private string path="/v2/providers/openapi/apis/api/v4/vendors/A000*****/outboundShippingCenters/3215908";
//replace with your own accessKey
private static string accessKey = "****";
//replace with your own secretKey
private static string secretKey = "****";
public void test(){
string method="PUT";
//replace with your own shipping location update json data
String strjson = "";
strjson = strjson + "{" + "\r\n";
strjson = strjson + " \"vendorId\": \"A000*****\"," + "\r\n";
strjson = strjson + " \"userId\": \"*****\"," + "\r\n";
strjson = strjson+" \"outboundShippingPlaceCode\": 3215908,"+ "\r\n";
strjson = strjson + " \"shippingPlaceName\": \"TEST0002\"," + "\r\n";
strjson = strjson + " \"placeAddresses\": [" + "\r\n";
strjson = strjson + " {" + "\r\n";
strjson = strjson + " \"addressType\": \"JIBUN\"," + "\r\n";
strjson = strjson + " \"countryCode\": \"KR\"," + "\r\n";
strjson = strjson + " \"companyContactNumber\": \"02-111-1111\"," + "\r\n";
strjson = strjson + " \"phoneNumber2\": \"02-111-1111\"," + "\r\n";
strjson = strjson + " \"returnZipCode\": \"32711\"," + "\r\n";
strjson = strjson + " \"returnAddress\": \"충청남도 금산군 추부면 추풍로\"," + "\r\n";
strjson = strjson + " \"returnAddressDetail\": \"111-11\"" + "\r\n";
strjson = strjson + " }" + "\r\n";
strjson = strjson + " ]," + "\r\n";
strjson = strjson + " \"remoteInfos\": [" + "\r\n";
strjson = strjson + " {" + "\r\n";
strjson = strjson+" \"remoteInfoId\": 2229966,"+ "\r\n";
strjson = strjson + " \"deliveryCode\": \"HYUNDAI\"," + "\r\n";
strjson = strjson + " \"jeju\": 5000," + "\r\n";
strjson = strjson + " \"notJeju\": 2500," + "\r\n";
strjson = strjson+" \"usable\": \"true\""+ "\r\n";
strjson = strjson + " }" + "\r\n";
strjson = strjson + " ]" + "\r\n";
strjson = strjson + "}" + "\r\n";
var uriBuilder = new UriBuilder(url+path);
uriBuilder.Scheme = schema;
uriBuilder.Port = port;
Uri finalUrl = uriBuilder.Uri;
Console.WriteLine(strjson);
try{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUrl.ToString());
request.Timeout = 10000;
request.Method = method;
byte[] data = Encoding.UTF8.GetBytes(strjson);
request.ContentType = "application/json;charset=UTF-8";
request.Headers["Authorization"] = GetHmac(method,"");
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
var response = (HttpWebResponse)request.GetResponse();
// Display the status ...
if (response.StatusCode == HttpStatusCode.OK){
Console.WriteLine("Response Status Code is OK and StatusDescription is: {0}",response.StatusDescription);
var responseStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader reader = new StreamReader( responseStream, encode );
string responseString = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(String.Format("Response: {0}", responseString));
}else{
Console.WriteLine("Response Status Code is Not OK and StatusDescription is: {0}",response.StatusDescription);
}
}
catch (WebException e)
{
Console.WriteLine("This program is expected to throw WebException on successful run." +
"\n\nException Message :" + e.Message);
if (e.Status == WebExceptionStatus.ProtocolError)
{
using (Stream data = e.Response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
catch(Exception e)
{
Console.WriteLine("The following Exception was raised : {0}",e.Message);
}
}
private string GetHmac(string method,string query)
{
string algorithm = "HmacSHA256";
string datetime = DateTime.Now.ToUniversalTime().ToString("yyMMddTHHmmssZ");
string message = GenerateFormattedMessage(datetime, method, query);
string signature = CreateTokenByHMACSHA256(message);
return GenerateFormattedHeader(algorithm, datetime, signature);
}
private string GenerateFormattedMessage(string datetime, string method, string query)
{
return String.Format("{0}{1}{2}{3}", datetime, method, path, query);
}
private String GenerateFormattedHeader(string algorithm, string datetime, string signature)
{
return String.Format("CEA algorithm={0}, access-key={1}, signed-date={2}, signature={3}", algorithm, accessKey, datetime, signature);
}
private String CreateTokenByHMACSHA256(string message)
{
secretKey = secretKey ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secretKey);
var hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
return ByteToString(hmacsha256.ComputeHash(messageBytes));
}
public string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("x2"); // hex format
}
return sbinary;
}
static void Main(string[] args)
{
sample_put myobj= new sample_put();
myobj.test();
}
}
}
4. C# DELETE 请求示例
using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ns_sample_delete
{
class sample_delete
{
private static string url = "https://api-gateway.coupang.com";
private static string schema = "https";
private static int port = 443;
private string path="/v2/providers/seller_api/apis/api/v1/marketplace/seller-products/48773047";
//replace with your own accessKey
private static string accessKey = "****";
//replace with your own secretKey
private static string secretKey = "****";
public void test(){
string method="DELETE";
var uriBuilder = new UriBuilder(url+path);
uriBuilder.Scheme = schema;
uriBuilder.Port = port;
Uri finalUrl = uriBuilder.Uri;
try{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUrl.ToString());
request.Timeout = 10000;
request.Method = method;
request.ContentType = "application/json;charset=UTF-8";
request.Headers["Authorization"] = GetHmac(method,"");
var response = (HttpWebResponse)request.GetResponse();
// Display the status ...
if (response.StatusCode == HttpStatusCode.OK){
Console.WriteLine("Response Status Code is OK and StatusDescription is: {0}",response.StatusDescription);
var responseStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader reader = new StreamReader( responseStream, encode );
string responseString = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(String.Format("Response: {0}", responseString));
}else{
Console.WriteLine("Response Status Code is Not OK and StatusDescription is: {0}",response.StatusDescription);
}
}
catch(WebException e)
{
Console.WriteLine("WebException Raised. The following error occured : {0}",e.Status);
Console.WriteLine(e.Message);
}
catch(Exception e)
{
Console.WriteLine("The following Exception was raised : {0}",e.Message);
}
}
private string GetHmac(string method,string query)
{
string algorithm = "HmacSHA256";
string datetime = DateTime.Now.ToUniversalTime().ToString("yyMMddTHHmmssZ");
string message = GenerateFormattedMessage(datetime, method, query);
string signature = CreateTokenByHMACSHA256(message);
return GenerateFormattedHeader(algorithm, datetime, signature);
}
private string GenerateFormattedMessage(string datetime, string method, string query)
{
return String.Format("{0}{1}{2}{3}", datetime, method, path, query);
}
private String GenerateFormattedHeader(string algorithm, string datetime, string signature)
{
return String.Format("CEA algorithm={0}, access-key={1}, signed-date={2}, signature={3}", algorithm, accessKey, datetime, signature);
}
private String CreateTokenByHMACSHA256(string message)
{
secretKey = secretKey ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secretKey);
var hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
return ByteToString(hmacsha256.ComputeHash(messageBytes));
}
public string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("x2"); // hex format
}
return sbinary;
}
static void Main(string[] args)
{
sample_delete myobj= new sample_delete();
myobj.test();
}
}
}