Ask Your Question

Revision history [back]

C# login/download data

We try to use C# to login and download data, login request is ok. and when we run download request , it show us unauthorized.

Code as following:

client.Credentials = new NetworkCredential("user", "password"); client.DownloadFile("http://rebrae.retsiq.com/contact/rets/login", @"C:\Users\wangch\test.txt");

Above section work fine and get response in file test.txt, but when we continue run following resuest, it shows unauthorized.

client.DownloadFile("http://rebrae.retsiq.com/contact/rets/Search?SearchType=Property&Class=RE_1&QueryType=DMQL2&Format=COMPACT-DECODED&StandardNames=0&Query=(L_Status=1_0,3_0),(L_InputDate=2017-07-25)&Select=L_Type_,L_DisplayId,L_City", @"C:\Users\wangch\test.txt1");

message as following: Unhandled Exception: System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at Program.Main() in \rae-edm-s-fs01\user$\wangch\My Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2\Program.cs:line 14