This code used to work and now it stopped working :/
I'm using the same code in other areas of my application and can call the link which works fine. But in this specific section of my application, it stopped working.
This link is valid and as stated above, it used to work.
Here is the code.
WebClient client = new WebClient();
string xmlResult = client.DownloadString(_workContext.AccountingWebServiceLink + "?action=updateprimary&xml=" + strXML);
I'm not sure if I need to increase the size of bytes in IIS? I suspect it might have something to do with the amount of data being sent.
Related
I've been scouring the web for a few hours looking for a solution to a problem that I need to resolve with a Xamarin.iOS app that I've been developing. Most, if not all, of our customers in the field need to upload pictures that they attach to work orders (tickets), which go to an FTP on our backend. My coworker developed the Android app and from him I took the current method, based off of an FtpRequest casted to an FtpWebRequest. It works 90% of the time and uploads to the server reasonably quick if the user has a decent connection.
The problem? I need 100% of the time, and I need it to be in the background. I found that NSUrlSession seems to be my best bet for solving this issue, as it's native to iOS and has support for backgrounding. My only issue is that when I try to create the UploadTask with session.CreateUploadTask, it says "cannot read file at (path)" where path is: file://var/mobile/Containers/Data/Application/850CB1FE-9C2D-456C-8B5F-921DC8D5CEF5/Documents/PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg. I've already confirmed that the file does exist on that path via printing out the file name using foreach( var file in Directory.EnumerateFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PolarisPictures2") ) Without using the file:// protocol, the error returns with "...is not a valid file:// url." So is it that I'm not accessing the file properly, or is it that there's simply no way to do what I'm trying to do?
And if there is no way, could anyone provide a solution that would best achieve the backgrounding capabilities that I require?
Appreciate it, guys.
Thanks.
Here's my code:
NSUrlSession session = null;
NSUrlSessionConfiguration config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("com.PolarisFTPUpload.BackgroundSession");
session = NSUrlSession.FromConfiguration(config, (NSUrlSessionDelegate)new MySessionDelegate(), new NSOperationQueue());
string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
docs = docs.Replace("/var", "file://var");
string filepath = System.IO.Path.Combine(docs, "PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg");
Console.WriteLine(filepath);
string UploadURLString = "ftp://myftpUser:myftpPass#myftpIP:Port/Pictures/Database" + "/" + DateTime.Today.Year + "/" + DateTime.Today.Month + "/" + DateTime.Today.Day + "/";
NSUrlSessionUploadTask uploadTask;
NSUrl uploadURL = NSUrl.FromString(UploadURLString);
NSUrlRequest req = NSUrlRequest.FromUrl(uploadURL);
uploadTask = session.CreateUploadTask(req, NSUrl.FromString(filepath));
Neither NSURLSession nor its predecessor, NSURLConnection, support the FTP PUT command. The only Apple API that supports FTP uploads is CFFTPStream, which is ancient, deprecated, and strongly discouraged for new development.
Either way, you should not under any circumstances use FTP. Period. It is fundamentally impossible to make FTP secure, because passwords are sent in cleartext across the network.
Instead, you should write a simple bit of PHP code on your web server that accepts file uploads, and use HTTPS uploads. Not only is that approach more secure, it is also supported with NSURLSession.
See Secure PHP File Upload Script for more information on the web site of things, and see Apple's NSURLSession Programming Guide for help with the uploads on the iOS side.
There should be three / with file prefix. The prefix should be file:///, not file://.
You can just use NSUrl.FromFilename(filepath) to get this url without replacing the prefix.
I am creating a Nancy Module that will eventually be hosted inside of a Windows Service. To start the Nancy hosting, I am using Nancy.Hosting.Self. Below is the code to start Nancy host.
string strHostProtocol = Convert.ToString(ConfigurationManager.AppSettings["HostProtocol"]);
string strHostIP = Convert.ToString(ConfigurationManager.AppSettings["HostIP"]);
string strHostPort = Convert.ToString(ConfigurationManager.AppSettings["HostPort"]);
//Here strHostProtocol="https", strHostIP = "192.168.100.88" i.e. System IPv4, strHostPort = "9003"
var url = strHostProtocol + "://" + strHostIP + ":" + strHostPort;
//url ="https://192.168.100.88:9003"
this.host = new NancyHost(new Uri(url));
this.host.Start();
Now once the windows service starts, it will start the above host and I could see this in netstat -a command. When I browse this in browser using https://192.168.100.88:9003 I will get proper response.
The problem arises when the same is browsed using its external IP. Say this system has been assigned with external IP of 208.91.158.66 and when I try browsing this as https://208.91.158.66:9003 I will just get a browser default loading progress continuosly which does not stop and without any error thrown. I have also added the below command and reserved URL successfully.
netsh http add urlacl url=https://192.168.100.88:9003/ user=everyone
But even after this the host cannot be browsed using external IP assigned to that system. Is there any restricting Nancy is putting up? Firewalls are turned off, defenders are turned off. Anyone has any idea on this?
UPDATE
The duplicate linked question talks about LAN but here I am trying through external IP and I've tried answer mentioned over there and also specified the same in question
Alright. This issue was also posted to GitHub Nancy Repo and below is what #Khellang had to say.
When you bind to https://192.168.100.88:9003, the
TcpListener/HttpListener won't listen on other interfaces. You either
have to bind to https://208.91.158.66:9003 or https://localhost:9003
and set RewriteLocalhost = true (default).
Further he also said that
If you also want to listen to requests coming to the external IP, yes.
Or you could use a wildcard, like https://+:9003/, https://*:9003/ or
https://localhost:9003/ (with RewriteLocalhost = true, this will
result in https://+:9003/). You can read more about them in the link I
posted.
and thanks to #TimBourguignon as he suggested the same in his comments. Hope this helps someone in future.
He has also suggested to read this link to know more about the Strong Wildcard and Weak Wildcard
I'm trying to obtain information from an online TFS server. The endpoint address for the OData service is: https://tfsodata.visualstudio.com/DefaultCollection.
I try and authenticate with this service using basic authentication as per:
_context = new TFSOData.TFSData(new Uri("https://tfsodata.visualstudio.com/DefaultCollection", UriKind.Absolute));
_context.SendingRequest2 += (object sender, SendingRequest2EventArgs e) =>
{
String creds = String.Format(CultureInfo.InvariantCulture, #"{0}\{1}:{2}", "mydomain", "myusername", "mypassword");
var bytes = Encoding.UTF8.GetBytes(creds);
var credstring = Convert.ToBase64String(bytes);
e.RequestMessage.SetHeader("Authorization", "Basic " + credstring);
};
Unfortunately I always get an exception returned from the eventArgs of my LoadCompleted Callback when I try and run a query on the Odata service and the query I'm running comes back empty when I know it shouldn't be.
{System.Security.SecurityException: Security error.}
Am I doing anything wrong security wise? I've tested my alternate credentials for this odata service in the browser and they work fine.
Just to reiterate, I'm doing this in a silverlight 5 project. I've had a similar method working in WPF before using e.requestheaders.add etc., but that's not available to me in Silverlight.
EDIT:
I've also noticed from using Fiddler that it's going to the clientaccesspolicy.xml file and then not continuing on to actually get the data I wanted from the service. Obviously this is the difference between the WPF app and the Silverlight app, however looking at the clientaccesspolicy.xml and crossdomain.xml files I can see that they are both allowing * and so I should be able to access what I want.
This was because the server was not setup to allow Cross Origin Resource Sharing.
First question!
Environment
MVC, C#, AppHarbor.
Problem
I am calling an openid provider, and generating an absolute callback url based on the domain.
On my local machine, this works fine if I hit http://localhost:12345/login
Request.Url; //gives me `http://localhost:12345/callback`
However, on AppHarbor where I'm deploying, because they are using non-standard ports, even if I'm hitting it at "http://sub.example.com/login"
Request.Url; //gives me http://sub.example.com:15232/callback
And this screws up my callback, because the port number wasn't in the original source url!
I've tried
Request.Url
Request.Url.OriginalString
Request.RawUrl
All gives me "http://sub.example.com:15232/callback".
Also to clear up that this isn't a Realm issue, the error message I am getting from DotNetOpenAuth is
'http://sub.example.com:14107/accounts/openidcallback' not under realm 'http://*.example.com/'.
I don't think I've stuffed that up?
Now, I'm about to consider some hacky stuff like
preprocessor commands (#IF DEBUG THEN PUT PORT)
string replace (Request.URL.Contains("localhost"))
All of these are not 100% solutions, but I'm sick of mulling over what could be a simple property that I am missing. I have also read this but that doesn't seem to have an accepted answer (and is more about the path rather than the authority). So I'm putting it towards you guys.
Summary
So if I had http://localhost:12345/login, I need to get http://localhost:12345/callback from the Request context.
And if I had "http://sub.example.com/login", I should get "http://sub.example.com/callback", regardless of what port it is on.
Thanks! (Sleep time, will answer any questions in the morning)
This is a common problem in load balanced setups like AppHarbor's - we've provided an example workaround.
Update: A more desirable solution for many ASP.NET applications may be to set the aspnet:UseHostHeaderForRequestUrl appSetting to true. We (AppHarbor) have seen several customers experience issues using it with their WCF apps, which is why we haven't enabled it by default and stil recommend the above solution for those situations. You can configure it using AppHarbor's "Configuration Variables" to inject the appsettings when deployed. More information can be found in this article.
I recently ran into an issue where I compared a URL to the current URL, and then highlighted navigation based on that. It worked locally, but not in production.
I had http://example.com/path/to/file.aspx as my file, but when viewing that file and running Request.Url.ToString() it produced https://example.com:81/path/to/file.aspx in a load balanced production environment.
Now I am using Request.Url.AbsolutePath to just give me /path/to/file.aspx, thus ignoring the schema, hostname, and port numbers.
When I need to compare it to the URL on each navigation item I used:
New Uri(theLink.Href).AbsolutePath
My initial thoughts are get the referrer variable and check if that includes a port, if so use it otherwise don't.
If that’s not an option because a proxy might remove the referrer header variable then you might need to use some client side script to get the location and pass it back to the server.
I'm guessing that AppHarbor use port forwarding to the IIS server so even though publicly the site is on port 80 IIS has it hosted on another port so it can't know what port the client connected on.
Something like
String port = Request.ServerVariables["SERVER_PORT"] == "80" ? "" : ":" + Request.ServerVariables["SERVER_PORT"];
String virtualRoot = Url.Content("~/");
destinationUrl = String.Format("http://{0}{1}{2}", Request.ServerVariables["SERVER_NAME"], port + virtualRoot, "/callback");
If you use the UrlBuilder class in the framework you can easly get around this. On the builder class if you set the port to -1 then the port number will be removed:
new UriBuilder("http://sub.example.com:15232/callback"){ Port = -1}
returns : http://sub.example.com/callback
To keep the port number on a local machine just check Request.IsLocal and don't apply -1 to the port.
I would wrap this into a extension method to keep it clean.
I see that this is an old thread. I had this issue running MVC5, on IIS 7.5, with an Apache proxy in front. Outside of the server, I would get "Empty Response", since the asp.net app gets the Url from apache with the custom port.
In order to have the app redirect to a subpath without including the "custom" port, forget the Response/Request objects, and use the Transfer method. For instance, if I want that users are automatically redirected to the login page in case they are not logged already:
if (!User.Identity.IsAuthenticated)
Server.TransferRequest("Account/Login");
I'm a C# game programmer with little web development experience.
I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. This is on the Windows Phone 7 using XNA. The target server is fairly limited and only supports PHP and classic ASP.
Since the CF on the WP7 only has access to a limited subset of networking commands, it's looking like an HttpWebRequest GET aimed at a script that saves the file will be the best option. The data I'm sending is small in size, and should be able to be passed as a parameter in the url.
I've been searching but have yet to find a complete example of this, which handles both the client and server side script (mainly the latter). This is close to what I'm looking for, except it has no mention of the server side script: Upload files with HTTPWebrequest (multipart/form-data)
The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/
But when attempting to use it I get an unhandled exception: "The remote server returned an error: (405) Method Not Allowed". This method seems the most promising so far, but I've yet to be able to debug this.
Unfortunately, I have a short amount of time to implement this, and as I said only a passing familiarity with web development. I'm not worried about maximum security or scalability as this is a temporary measure to collect feedback internally. Basically, I just need the quickest thing that works. ;)
Any help would be fantastic!
I've solved it. First off, PHP wasn't supported on my server (just now learning that PHP and ASP are can't be used on the same server, depending on whether it's on Linux or Windows - like I said, web development noob here!). I switched to ASP and, after digging through the docs, wrote this script:
<%
dim theData, theFileName
set theData=Request("data")
set theFileName=Request("filename")
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath(theFileName+".txt"))
tfile.WriteLine(theData)
tfile.Close
set fname=nothing
set fs=nothing
set theData=nothing
set theFileName=nothing
%>
This C# code uploads the file:
const string cAddress = "http://site.com/folder/upload.asp";
string fileName = foo;
string data = bar;
string address = cAddress + "?filename=" + fileName + "&data=" + data;
uploadRequest = (HttpWebRequest) HttpWebRequest.Create(address);
uploadRequest.Method = "GET";
uploadRequest.GetResponse();
Hope this helps someone else looking for an example of how to do this!
But you have the METHOD as GET instead of POST. You can't upload a file to a website by passing the file path to the Query String.