Hi ,
I'm trying to use CUMI API for sending Message to Unity Connection.
I'm following DocWiki example,but all the time get Response from Unity:
The remote server returned an error 400 Bad RequestHere is the Code:
private void button1_Click(object sender, EventArgs e)
{
string str;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("
https://10.0.0.201:8443/vmrest/messages");
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
byte[] bytes = Encoding.UTF8.GetBytes("******:*******");
string base64 = Convert.ToBase64String(bytes);
req.Method = "Post";
req.Accept = "application/xml";
req.ProtocolVersion = System.Net.HttpVersion.Version11;
req.ContentType = "multipart/mixed";
req.Headers.Add("Authorization", "Basic " + base64);
byte[] sentData = Check();
req.ContentLength = sentData.Length;
Stream sendStream = req.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.WriteTimeout = 10;
sendStream.Close();
try
{
WebResponse res = req.GetResponse();
StreamReader readl = new StreamReader(res.GetResponseStream());
str = readl.ReadLine();
MessageBox.Show(str);
readl.Close();//
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public byte[] Check()
{
string data;
TextReader tr = new StreamReader(File.OpenRead("c:\\1.txt"));
Chilkat.Mime mime = new Chilkat.Mime();
bool success;
success = mime.UnlockComponent("Anything for 30-day trial");
if (success == false)
{
MessageBox.Show("Failed to unlock component");
return mime.GetMimeBytes();
}
// Initialize this MIME object as a multipart/mixed:
success = mime.NewMultipartMixed();
mime.ContentType = "multipart/mixed";
// Now insert some files:
data = tr.ReadLine();
Chilkat.Mime textBody = new Chilkat.Mime();
textBody.SetBodyFromPlainText(data);
textBody.ContentType = "application/json";
success = mime.AppendPart(textBody);
if (success == false)
{
MessageBox.Show(mime.LastErrorText);
return mime.GetMimeBytes();
}
data = tr.ReadLine();
Chilkat.Mime textBody1 = new Chilkat.Mime();
textBody1.SetBodyFromPlainText(data);
textBody1.ContentType = "application/json";
success = mime.AppendPart(textBody1);
if (success == false)
{
MessageBox.Show(mime.LastErrorText);
return mime.GetMimeBytes();
}
/*
Chilkat.Mime textBody2 = new Chilkat.Mime();
textBody2.SetBodyFromFile("C://2020.wav");
textBody2.ContentType = "audio/wav";
success = mime.AppendPart(textBody2);
if (success == false)
{
MessageBox.Show(mime.LastErrorText);
return mime.GetMimeBytes();
}
*/
return mime.GetMimeBytes();
}
DATA Of Message Sent To Server in JSON
{"Subject":"subscriber send message test","ArrivalTime":"0","FromSub":"false"}
{"Recipient":{"Type":"TO","Address":{"ObjectId":"fae5a39d-2ef3-490b-8750-e4a54193cd73","Type":"SUBSCRIBER"}}}
The response from EchoServer is:
POST / HTTP/1.1
Accept: application/xml
Content-Type: multipart/mixed
Authorization: Basic ********
Host: 127.0.0.1:8081
Content-Length: 580
Expect: 100-continue
Connection: Keep-Alive
Content-Type: multipart/mixed;
boundary="------------070200000807000300060601"
This is a multi-part message in MIME format.
--------------070200000807000300060601
Content-Transfer-Encoding: 7bit
Content-Type: application/json
{"Subject":"subscriber send message test","ArrivalTime":"0","FromSub":"false"}
--------------070200000807000300060601
Content-Transfer-Encoding: 7bit
Content-Type: application/json
{"Recipient":{"Type":"TO","Address":{"ObjectId":"fae5a39d-2ef3-490b-8750-e4a541
93cd73","Type":"SUBSCRIBER"}}}
--------------070200000807000300060601--
Any Help or Example?
Thanks