Helo,
I want to ask you, if I can send tile notification from one phone to another phone and vice versa using WCF service?
Thanks
To send push to other phone you have to have ChannelURI. There is good article about it
http://msdn.microsoft.com/en-us/library/hh221549.aspx
When you have this adress you have to send special XML data
string textTitle = tbxTitle.Text;
string textSubtitle = tbxSubtitle.Text;
string deviceUri = tbxUri.Text;
string msg =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + textTitle + "</wp:Text1>" +
"<wp:Text2>" + textSubtitle + "</wp:Text2>" +
"</wp:Toast>" +
"</wp:Notification>";
byte[] msgBytes = new UTF8Encoding().GetBytes(msg);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "text/xml";
request.ContentLength = msg.Length;
request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
request.Headers["X-WindowsPhone-Target"] = "toast";
request.Headers["X-NotificationClass"] = "2";
Stream requestStream = request.GetRequestStream();
requestStream.Write(msgBytes, 0, msgBytes.Length);
requestStream.Close();
Related
I want to add Mailchimp subscriber via Mailchimp api in MVC controller when form register in database.
So I use below code with api key trully wrote.
public void AddContact(string email, string name, string surname, string listKey = "7f0c43ed8f")
{
string apiKey = "apikey comes here";
string url = string.Format("https://us8.api.mailchimp.com/3.0/lists/{0}/members/", listKey);
string data = "{\"email_address\": \"" + email + "\",\"status\": \"subscribed\",\"merge_fields\": {\"FNAME\": \"" + name + "\",\"LNAME\": \"" + surname + "\"}}";
WebRequest myReq = WebRequest.Create(url);
myReq.Method = "POST";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/json; charset=UTF-8";
string usernamePassword = "anystr:" + apiKey;
UTF8Encoding enc = new UTF8Encoding();
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(enc.GetBytes(usernamePassword)));
using (Stream ds = myReq.GetRequestStream())
{
ds.Write(enc.GetBytes(data), 0, data.Length);
}
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
}
But this code works sometimes. Sometimes it gives "400 Bad Request" error and I can not find any error or solution about this problem.
I appreciate your help.
Thanks in advance.
this my code for create session and upload file in onedrive
string url = "https://api.onedrive.com/v1.0/drive/root:/" + "Nopbackup" +":/" + "" + fileName +":/upload.createSession" + "";
var result = string.Empty;
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
//request.ContentType = "application/json";
request.ContentType = "contentType";
//request.ac = "application/json";
request.Headers.Add("Authorization", tokenType + " " + accessToken);
request.ContentLength = 0;
//byte[] requestData = Encoding.UTF8.GetBytes(url);
//using (Stream st = request.GetRequestStream())
// st.Write(requestData, 0, requestData.Length);
WebResponse response = request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
result = streamReader.ReadToEnd();
its giving server bad request 400 error
I think the issue is the URL you're POSTing with. It looks like this:
string url = "https://api.onedrive.com/v1.0/drive/root:/" + "Nopbackup" +":/" + "" + fileName +":/upload.createSession" + "";
Which turns out to be
https://api.onedrive.com/v1.0/drive/root:/Nopbackup:/filename:/upload.createSession
You've got the colon syntax a little messed up, and the API doesn't know what to do with that (hence the bad request).
Try building the URL like this instead:
string url = "https://api.onedrive.com/v1.0/drive/root:/" + "Nopbackup" + "/" + fileName +":/upload.createSession";
And I think things will work better for you.
I made a web service that can send push notification depending on this tutorial. When i call the function from a web form, it works perfectly. However, when i call the same function from phone i don't receive the notification at all although the response is "Received | Active | Connected". The function is like this :
[WebMethod]
public void SendNotification(string Title, string msg, string url)
{
try
{
string subscriptionUri = GetNotificationUrl();
HttpWebRequest sendNotificationRequest = (HttpWebRequest) WebRequest.Create(subscriptionUri);
sendNotificationRequest.Method = "POST";
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + Title + "</wp:Text1>" +
"<wp:Text2>" + msg + "</wp:Text2>" +
"<wp:Param>" + url + "</wp:Param>" +
"</wp:Toast></wp:Notification>";
byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse) sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
WriteLog(notificationStatus + " | " + notificationChannelStatus + " | " +
deviceConnectionStatus);
}
catch
{
}
}
When i try to read the stream i get timeout exception. The response i get from the server is the mp3 file and i don't have a clue how to save it as a mp3.
WebClient webClient = new WebClient();
int selectedIndex = listBox2.SelectedIndex;
JObject o = JObject.Parse(result);
string SongID = o["SongID"].ToString();
string SongName = o["SongName"].ToString();
string Year = o["Year"].ToString();
string AlbumName = o["AlbumName"].ToString();
textBox4.Text = SongID + Environment.NewLine + SongName + Environment.NewLine + Year + Environment.NewLine + AlbumName;
string stream = Webpost("{\"header\":{\"token\":\"" +prepToken("getStreamKeyFromSongIDEx", ":chickenFingers:")+"\",\"privacy\":0,\"country\":{\"DMA\":0,\"CC1\":0,\"IPR\":0,\"CC2\":0,\"CC3\":2305843009213694000,\"ID\":190,\"CC4\":0},\"uuid\":\"8E5D1ABD-EE1B-4498-B960-8E46077E8ED4\",\"client\":\"jsqueue\",\"session\":\""+session+"\",\"clientRevision\":\"20130520\"},\"method\":\"getStreamKeyFromSongIDEx\",\"parameters\":{\"mobile\":false,\"prefetch\":false,\"songID\":"+SongID+",\"type\":0,\"country\":{\"DMA\":0,\"CC1\":0,\"IPR\":0,\"CC2\":0,\"CC3\":2305843009213694000,\"ID\":190,\"CC4\":0}}}", "http://*****.com/more.php?getStreamKeyFromSongIDEx", "POST", "text/plain");
JObject ss = JObject.Parse(stream);
string streamkey = ss["result"]["streamKey"].ToString();
string ip = ss["result"]["ip"].ToString();
textBox4.Text += Environment.NewLine+ "downloiad url: " + ip + Environment.NewLine + "stream key: " + streamkey;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ip + "/stream.php");
request.Method = "POST";
string postData = "streamKey="+ streamkey;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = postData.Length;
request.Host = ip;
request.Accept = "*/*";
request.Headers.Add("Origin", string.Format("http://*********.com"));
request.Headers.Add("Accept-Language", string.Format("sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4"));
request.Headers.Add("Accept-Encoding", string.Format("gzip,deflate,sdch"));
request.Headers.Add("Cookie", string.Format("ismobile=no; PHPSESSID="+ session + "__utma=111479378.517776767.1385046829.1385170025.1385750338.12; __utmb=111479378.14.9.1385751143490; __utmc=111479378; __utmz=111479378.1385157436.6.2.utmcsr=nettech.wikia.com|utmccn=(referral)|utmcmd=referral|utmcct=/wiki/*****_Internal_API"));
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
textBox4.Text += Environment.NewLine + "headers: " + Environment.NewLine + request.Headers;
textBox4.Text += Environment.NewLine + "StreamKey:" + byteArray;
Stream respStream = request.GetRequestStream();
you can try something like this
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("mp3 url");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
byte[] buffer = new byte[32768];
using (FileStream fileStream = File.Create("yourfullnamepath.mp3"))
{
while (true)
{
int read = receiveStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
fileStream.Write(buffer, 0, read);
}
}
So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type=\"hidden\" name=\"id\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("\r\n" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"title\"" + "\r\n");
sb.Append("\r\n");
sb.Append(title);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"tags\"" + "\r\n");
sb.Append("\r\n");
sb.Append(tags);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"bodytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(text);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarycheckbox\"" + "\r\n");
sb.Append("\r\n");
sb.Append("on");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(summary);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"remLen\"" + "\r\n");
sb.Append("\r\n");
sb.Append("125");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"category\"" + "\r\n");
sb.Append("\r\n");
sb.Append("1");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"trackback\"" + "\r\n");
sb.Append("\r\n");
sb.Append("");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"url\"" + "\r\n");
sb.Append("\r\n");
sb.Append(link);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"phase\"" + "\r\n");
sb.Append("\r\n");
sb.Append("2");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"randkey\"" + "\r\n");
sb.Append("\r\n");
sb.Append(key2);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"id\"" + "\r\n");
sb.Append("\r\n");
sb.Append(id2);
sb.Append("\r\n--" + boundary + "--" + "\r\n");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\\Users\\awasthi\\Downloads\\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
The format of multipart/form-data requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.