How do I get HTTP StatusCodes other than 200 on Windows Phone 7? - windows-phone-7

Currently with Windows Phone 7 if I access a page that returns a StatusCode of 500 or something besides 200 OK I get a message saying the NotFound. In silverlight there are a couple options for handling this:
Use SilverlightFaultBehavior if you control the REST Service (I don't though)
Use HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
I'm wondering if there are any options like this for the Windows Phone 7.

All you have to do is catch the WebException, and access the response within that. Then you can get at the status code, response stream etc.
If that's no use to you, please post the code you're using and what you're trying to do - it's hard to know exactly what you've tried at the moment.

HttpWebResponse includes a StatusCode property. Can't you use that?
webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
webResponse.StatusCode;

Related

How to send an email with multiple attachments from Gmail using API client library for .NET

My app uses Google API client library for .NET to send emails with attachments.
When using Send(), I'm facing some limitations when it comes to file size of the attachments. So, I guess switching to Resumable upload as upload method may help. But it's pretty much undocumented.
Looking into source code, I guess using different Send() overload may be the way forward, but I can't figure out how to use it properly.
So, instead of attaching the files into message and calling it like this:
var gmailResult = gmail.Users.Messages.Send(new Message
{
Raw = base64UrlEncodedMessage
}, "me").Execute();
I should not attach the files to message and do something like following?
var gmailResult = gmail.Users.Messages.Send(new Message
{
Raw = base64UrlEncodedMessage
}, "me", fileStream, contentType).Upload();
The second version does not return any API error, but does nothing. I'm obviously missing something here.
How do I attach more than one attachment?
This is kind of an old question, but putting an answer here just in case anyone else needs it:
I was able to achieve this by converting my mime message into a stream (attachment(s) included), and then calling this overload on Send:
UsersResource.MessagesResource.SendMediaUpload googleSendRequest = service.Users.Messages.Send(null, "youremail#gmail.com", mimeMessageStream, "message/rfc822");
IUploadProgress created = googleSendRequest.Upload();
This will upload all of the attachments with the email message content and then send it off. I was able to send two 5 megabyte attachments in an email. Previously I was not able to send even one of those via the other Send method that takes in a base64 encoded mime message.

Getting a response uri

I'm new to Windows phone 7 application development. I'm currently developing an app in which I wish to do a HTML request and display the result obtained in Web browser. For example, Suppose I give the below URI
"http://m.imdb.com/find?q="+search_string (where search_string is a variable)
I want to take the result obtained from this and display it on the web browser. I've been searching regarding this for past 1 day... Didn't get any fruitful results. So please redirect me either to a suitable tutorial page or please give a sample code?
WebBrowserTask wbt = new WebBrowserTask();
wbt.URL = "http://m.imdb.com/find?q="+search_string
wbt.Show();
this will allow you to launch the url in the browser.
WebBrowserTask

Find out what exactly XMLHttpRequest is requesting

We're developing a cross-platform application using PhoneGap. At one point, it makes an AJAX request to a relative path, which works fine on both Android and iOS, but not Windows Phone - it gets a 404 response. An absolute URL works fine. The following code:
var a = document.createElement('a');
a.setAttribute('href', 'personalData.html');
console.log(a.href);
also resolves to the correct absolute URL. However, the following:
var xhr = new XMLHttpRequest();
xhr.open("GET", "personalData.html", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr);
}
};
returns 404. I want to find out what's going on. Is there any way to know what absolute path XHR is requesting?
XMLHttpRequest is a JavaScript object that was designed by Microsoft and adopted by Mozilla, Apple, and Google, it's not related to Phonegap.
https://developer.mozilla.org/en/DOM/XMLHttpRequest
Said this, you could try using an http Proxy like Fiddler to view all http trafic.
http://www.fiddler2.com/fiddler2/
Best regards.
In these cases, Fiddler Web Debugger is unbeatable. It will tell you exactly what the request is doing.
It also works with the Windows Phone emulator. To debug an actual device, setup FIddler to accept external connections and assign Fiddler as a proxy on the phone.
I have done both scenarios, works fine.
Give it a shot.
I have try your code in my project (Phonegap/WinPhone7) and your code didn't get anything till I initialized the request (xhr.send();).
I have no idea how you make request without this method.

How do you disable caching with WebClient and Windows Phone 7

I am making a call to a REST web service and the mobile app is retrieving the results from its cache and not going to the server.
I have seen other suggested fixes (similar issue and similar issue2) but the Cache property is not available in silverlight 4.
Does anyone have an idea of how to force silverlight 4 on windows phone 7 to make a request and not hit the cache?
Although not ideal, a easy solution is to send something like the field "junk" with the value DateTime.Now. That way, a value is always brand new, and will never get cached. If you were doing this in a standard querysting for example:
"&junk=" + DateTime.Now;
I've hit this problem too on overflow 7 talking to StackApps - the only thing I could think of was to add an addition random variable to the end of the HTTP/REST request.
The most proposed solution is the same as William Melani's.
But it is not ideal and some services reject requests with unknown parameters or any parameter. In this case it is cleaner and more reliable to use the IfModifiedSince header as follows:
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri(bitstampUrl));
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
worked for me

C++ interface version of HttpWebRequest and HttpWebResponse

We are wondering how to use HttpWebRequest and HttpWebResponse .net framework Class in ATL c++ project is their any interface exposed for webrequest class in C++, currently we cannot have a c# project so we are looking for alternative interface.
Any help will be greatly appreciated.
Ramanand.
You have the following options:
1) Write your managed HttpWebRequest code into a C# file, and compile it as a DLL. Use RegAsm.exe to register it as a COM object. Use the COM object from the C/C++ application.
2) As Michael has suggested above, use Managed C++ to write the code, and interop/interface with other parts of your C/C++ code.
3) Dont use managed code! Use the platform specific libraries - for eg, WinHTTP from Microsoft is well tested, and supported for both client side and server side operations. You can also use Wininet which is what is used by Internet Explorer, however it is not recommended for use in Middle-tier scenarios.
So,unless you really need something that is offered by System.Net managed code namespace that is not available on Wininet/WinHTTP, I would not opt for managed code. Managed code will bring in memory and cpu overhead that is really not needed if all you are doing is downloading web pages.
please refer to this post: How do you make a HTTP request with C++?
libcurl is recommended in many posts.
You have to use C++/CLI. A code snippet might look something like this.
// Open a connection
System::Net::HttpWebRequest ^_HttpWebRequest = safe_cast<System::Net::HttpWebRequest^>(System::Net::HttpWebRequest::Create(_URL));
_HttpWebRequest->AllowWriteStreamBuffering = true;
// You can specify additional header values:
_HttpWebRequest->UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
_HttpWebRequest->Referer = "http://www.google.com/";
// set timeout for 20 seconds (Optional)
_HttpWebRequest->Timeout = 20000;
// Request response:
System::Net::WebResponse ^_WebResponse = _HttpWebRequest->GetResponse();
// Open data stream:
System::IO::Stream ^_WebStream = _WebResponse->GetResponseStream();
// Do something with stream
_tmpImage = Image::FromStream(_WebStream);
// Cleanup
_WebResponse->Close();
_WebResponse->Close();

Resources