Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

im getting An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
{"The remote server returned an error: (400) Bad Request."}

C#
private string FileManagerServiceUrl
      {
          get
          {
              return "http://localhost/FileSync.SyncService/TransferService.svc";
          }
      }


this is my service url..

am calling this using..
C#
string requestFile = @"D:\TestFolder\item.zip";
           string fileSavePath = @"D:\TestFolder\copyContainer\testfile.zip";
           DownLoadFileFromRemoteLocation(requestFile, fileSavePath);


and downloadfilefromRemoteLocation look like
C#
private void DownLoadFileFromRemoteLocation(string downloadFileLocation, string downloadedFileSaveLocation)
      {
          string serviceUrl = string.Format("{0}/RetrieveFile/{1}", FileManagerServiceUrl, downloadFileLocation);
          var request = WebRequest.Create(serviceUrl);
          request.UseDefaultCredentials = true;
          request.PreAuthenticate = true;
          request.Credentials = CredentialCache.DefaultCredentials;
          using (var response = request.GetResponse())
          {
              using (var fileStream = response.GetResponseStream())
              {
                  CreateDirectoryForSaveLocation(downloadedFileSaveLocation);
                  SaveFile(downloadedFileSaveLocation, fileStream);
              }
          }


      }



and RetrieveFile method like

C#
public Stream RetrieveFile(string path)
      {
          if (WebOperationContext.Current == null) throw new Exception("WebOperationContext not set");

          // As the current service is being used by a windows client, there is no browser interactivity.
          // In case you are using the code Web, please use the appropriate content type.
          var fileName = Path.GetFileName(path);
          WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
          WebOperationContext.Current.OutgoingResponse.Headers.Add("content-disposition", "inline; filename=" + fileName);

          return File.OpenRead(path);
      }


im new to wcf and web... actually what is the issue issue.?
is any misconfiguration of web.config?
Posted
Comments
Put the url in browser and see what is the result.
Indhukanth Pv 7-Dec-14 4:46am    
Hi Tadit,
its showing "A potentially dangerous Request.Path value was detected from the client (:)." my url look likes
http://localhost/FileSync.SyncService/TransferService.svc/RetrieveFile/D:%5CTestFolder%5Citem.zip..

RetrieveFile: is the service contractor ie,
public Stream RetrieveFile(string path)

path : is uri path..

im using another client application which uses following code to download

private void DownLoadFileFromRemoteLocation(string downloadFileLocation, string downloadedFileSaveLocation)
{

string serviceUrl = string.Format("{0}/RetrieveFile/{1}", FileManagerServiceUrl, downloadFileLocation);
var request = WebRequest.Create(serviceUrl);
using (var response = request.GetResponse())
{
using (var fileStream = response.GetResponseStream())
{
CreateDirectoryForSaveLocation(downloadedFileSaveLocation);
SaveFile(downloadedFileSaveLocation, fileStream);
}
}


private string FileManagerServiceUrl
{
get
{
return "http://localhost/FileSync.SyncService/TransferService.svc";
}
}
You need to encode all the special characters.
Indhukanth Pv 7-Dec-14 10:43am    
is special character in url?
can you briefly explain?
There are colons in the URL. I think that need to be encoded.

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