Need Extreme Help
-
Wednesday, June 22, 2011 2:39 PM
public class GoogleUrlShortner { public static GoogleUrlReply Shorten(string longUrl, string apiKey = "") { string data = "{\"longUrl\":\"" + longUrl + "\"}"; string gUrl = "https://www.googleapis.com/urlshortener/v1/url"; if (!string.IsNullOrEmpty(apiKey)) gUrl += "?key=" + apiKey; string response = Post(gUrl, data); return FromJSON<GoogleUrlReply>(response); } public static GoogleUrlReply Expand(string shortUrl, string apiKey = "") { string gUrl = "https://www.googleapis.com/urlshortener/v1/url?shortUrl=" + shortUrl; if (!string.IsNullOrEmpty(apiKey)) gUrl += "&key=" + apiKey; string response = Get(gUrl); return FromJSON<GoogleUrlReply>(response); } public static string Get(string url) { WebClient web = new WebClient(); return web.(url); } private static string Post(string url, string data) { System.Net.WebRequest request1 = WebRequest.Create(url); request1.Method = "POST"; request1.ContentType = "application/json"; byte[] byteData = Encoding.UTF8.GetBytes(data); request1.ContentLength = byteData.Length; using (Stream s = request1.GetRequestStream()) { s.Write(byteData, 0, byteData.Length); s.Close(); } string replyData; using (HttpWebResponse response = (HttpWebResponse)request1.GetResponse()) { using (Stream dataStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { replyData = reader.ReadToEnd(); } } } return replyData; } private static T FromJSON<T>(string input) { MemoryStream stream = new MemoryStream(); try { DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof(T)); stream = new MemoryStream(Encoding.UTF8.GetBytes(input)); T obj = (T)jsSerializer.ReadObject(stream); return obj; } finally { stream.Close(); stream.Dispose(); } }
I am getting errors in webclient, webresponse and otherspls rectify for me these erros...am extremely frustrated with WP7 coding
All Replies
-
Wednesday, June 22, 2011 6:21 PM
so... what errors are you getting?
-
Thursday, June 23, 2011 5:10 AM
We are here to help you but you need to be more factual and provide details of your situation in a more logical way.
-
Thursday, July 21, 2011 8:42 AM
Just guessing here, but typically web service calls are asynchronous and you need a callback function. You cannot simply read the response from the same method that you issued the request from. Also, WebClient is easier to use. Here is an example:
http://blogs.msdn.com/b/silverlight_sdk/archive/2008/04/01/using-webclient-and-httpwebrequest.aspx
Tom

