Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Hi,

I want to download the document from below mentioned URL. When i try to download directly in the IE URL, it will be asking username and password credential to open the document.

But, when i try to download through application i am getting below error.

"The remote server returned an error: (401) Unauthorized."

ASP.NET Code:

downloadbutton_click event

URIFile = http://sr1250kl.vrmnet.vrm.intranet/A1/servlet/Download?auth=basic&event_name=k1_view&_file=emparea&id=0000008763&version=01

Dim client As New WebClient()
client.Credentials = CredentialCache.DefaultCredentials
client.UseDefaultCredentials = True
client.Credentials = New NetworkCredential("XXXXXX", "XXXXXXX", "vrmnet.vrm.intranet")
Dim buffer As Byte() = client.DownloadData(URIFile )
Dim download As String = Encoding.ASCII.GetString(buffer)
Console.WriteLine(download)
Console.WriteLine("Download successful.")
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)

The above code is not working when i click the download button using above code. Error : "The remote server returned an error: (401) Unauthorized."

How to download the file..?


What I have tried:

URIFile = http://sr1250kl.vrmnet.vrm.intranet/A1/servlet/Download?auth=basic&event_name=k1_view&_file=emparea&id=0000008763&version=01

Dim client As New WebClient()
client.Credentials = CredentialCache.DefaultCredentials
client.UseDefaultCredentials = True
client.Credentials = New NetworkCredential("XXXXXX", "XXXXXXX", "vrmnet.vrm.intranet")
Dim buffer As Byte() = client.DownloadData(URIFile )
Dim download As String = Encoding.ASCII.GetString(buffer)
Console.WriteLine(download)
Console.WriteLine("Download successful.")
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Posted
Updated 28-Jun-16 19:26pm
Comments
gani7787 14-Jun-16 0:19am    
Any solution for this...waiting for reply from anyone...

1 solution

C#
Finally i did R&D i found the solution.it's very simple code.

Dim fileName As String = grd.DataKeys(gvrow.RowIndex).Values(0).ToString()

ScriptManager.RegisterStartupScript(Me, Page.GetType(), "Open", "window.open('" + fileName + "');", True)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900