error writing file on remote server in WCF - wcf

I'm creating a WCF (C#, VS2010) for my silverlight app, I'm trying to create a file on my remote server (where WCF exists), but I get an unknown error, I just get "Server not found" error in WCF function complete exception message, I use following codes to create a file on my remote server (I use a VDS, so I think I don't have permission problems, I use remote desktop on my server)
using (StreamWriter outfile =
new StreamWriter("mytest.htm"))
{
outfile.Write(body);
}
what is going wrong here? is it possible that I have permission problems? I run my WCF on local system and it creates file successfully, but there is no luck when it is uploaded to remote server
should I use Server.MapPath for giving address of my file?

Please use this:
System.Net.FileWebRequest request = (System.Net.FileWebRequest)System.Net.FileWebRequest.CreateDefault(new Uri("\\\\remotelocation\\MyText.txt"));
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
byte [] fileContents = Encoding.UTF8.GetBytes("test_content");
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

Related

Restsharp: Returns 0 content with error "unable to read data from the transport connection"

Background: Console app built in .Net 4.6.2
Dll: Restsharp 106.15.0.0
Mono: Mono JIT compiler version 6.12.0.122
API Endpoint: http server (not https)
At first I thought I was going mad as all was working okay on windows but when I run my console app on Debian (v9), it will constantly fail with the error: unable to read data from the transport connection. I've narrowed it down to restsharp and not a api/server issue.
When I run a debugger in parallel (HTTP Debugger) I can see the request go out from my console app on Debian and I can also see the endpoint return a result with content! This content does not come into restsharp and so restsharp gives me a content-length of 0 and an error message of:
unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
Any ideas please?
Code to trigger restsharp:
Dim myUri As New Uri(url)
Dim client = New RestSharp.RestClient(myUri)
Dim request = New RestRequest With {.Method = Method.GET}
request.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
request.AddHeader("Connection", "keep-alive")
Dim response As RestResponse = client.Execute(request)
Dim strResponse as String = response.Content)
(I've also tried without sending the headers - makes no difference, same error message)
Thanks

PayPal works fine on localhost, but fails while works on aws server

I have integrated paypal to my MVC4 application. PayPal dll version 1.5.0.0, NewtonJson dll version 6.0.0.0
At first i got exception at localhost while getting access token
Dictionary<string, string> sdkConfig = new Dictionary<string, string>();
sdkConfig.Add("mode", "sandbox");
string accessToken = new PayPal.Api.OAuthTokenCredential("MyClientId", "MySecretId", sdkConfig).GetAccessToken();
Exception was
Invalid HTTP response: The request was aborted: Could not create SSL/TLS secure channel.
From stackoverflow I got a fix
System.Net.ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
System.Net.ServicePointManager.DefaultConnectionLimit = 9999;
Its currently working fine on my localhost, but getting exception when uploaded and run on AWS windows instance.
Retried 3 times.... Exception in PayPal.HttpConnection.Execute(). Check log for more details.
Can somebody help me on this ?
UPDATE
I have checked with uploading the same code in mochahost server. Its working perfectly there too
My EC2 instance is Windows Server 2008 DataCenter, 32 bit with IIS7
Make sure TCP443 is open on any elastic load balancer (ELB) you are using and in the security group assigned to the EC2 instance. In the OS make sure TCP443 is allowed with Windows Advanced Firewall.

FTP Directory browsable in Internet Explorer but not in Windows Explorer; VB.NET code to fake web browsing

I have a strange case where a client's FTP server is fully browsable in a web browser, but not in a file explorer.
This is what I see in IE:
And this is what I see in Windows Explorer:
What I'm really trying is to write code that reads the list of files from this ftp directory:
Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpServer), FtpWebRequest)
ftpRequest.Credentials = New NetworkCredential(ftpServerUsername, ftpServerPassword)
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse(), FtpWebResponse)
Dim ftpResponseStream As Stream = ftpResponse.GetResponseStream()
Dim ftpResponseStreamReader As StreamReader = New StreamReader(ftpResponseStream)
Console.WriteLine(ftpResponseStreamReader.ReadToEnd())
ftpResponseStreamReader.Close()
ftpResponseStream.Close()
ftpResponse.Close()
But the code fails with a 451 error:
The remote server returned an error: (451) Local error in processing.
(Details: 451 requested action aborted: local error in processing)
Questions:
Why is the FTP browsable on IE but now in Windows? Should I tell my
client to change some properties on the FTP setup to make it
directory-browsable in Windows?
Is (1) necessary? Instead is it
possible to add/change my code to imitate web-browsing so that the
list of files can be read?
CiarĂ¡n's comment helped access files via Windows Explorer: the URL of the format ftp://username:password#IPAddress/ worked.
For the code, however, a slash "/" at the end of the URL did the trick!
I changed the directory name from ftp://server/directory to ftp://server/directory/ and BOOM! VB was able to retrieve the list of files!
I tried the same in IE, and here's what I get:
ftp://193.XX.XX.XX/flog:
ftp://193.XX.XX.XX/flog/: (note the "/" at the end of directory name)
Anyone else stumbling here with a (451) Local error in processing can try this and see if it helps!
Additional Note:
The URL of the format ftp://username:password#IPAddress/ (again, note the ending "/") also works in code. With this, you can skip the line ftpRequest.Credentials = ....

'Unable to connect to the remote server' when connected to corporate intranet

var request = (HttpWebRequest)WebRequest.Create("http://www.bbc.co.uk");
string stream;
using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.Default))
{
stream = reader.ReadToEnd();
}
If I'm connected to a corporate intranet I get "Unable to connect to the remote server". When I'm not connected it connects fine. Why might this be?
(The intranet is not the BBC's. That's just a random URL I picked.)
I looked up one of the proxies that IE is using and used that. It worked!

Uploading files from .exe to server

I wanted to know how I would be able to upload files to my server from my WinForms .exe application. I tried: My.Computer.Network.UploadFile(myFile, myServer) but I get this error 'The remote server returned an error: (404) Not Found.' Anyone know another way of uploading files to my server from a .exe? Thanks.
I'm using VB and .Net Framework 4
Here is another way, but it sounds like you are uploading to the wrong page (HTTP error = 404). Check your URL.
Dim fileName As String = AskUserForFileName()
Dim web As New WebClient()
web.UploadFile(uriString, fileName)