0

我正在使用 StreamReader 通过 WebRequest 从 C# 调用 PHP (TCPPDF) 中的例程。PDF 文件作为流返回并存储在字符串 (obv) 中。我知道返回到字符串的数据实际上是一个 PDF 文件,因为我已经在 PHP 中对其进行了测试。我很难将字符串写入文件并实际上在 C# 中获取有效的 PDF。我知道这与我尝试对文件进行编码的方式有关,但我尝试过的几件事导致“今天不行,Padre”(即他们没有工作)

这是我用来执行请求的类(感谢用户'Paramiliar'对于我正在使用/借用/偷窃的示例):

    public class httpPostData
    {
        WebRequest request;
        WebResponse response;

        public string senddata(string url, string postdata)
        {

            // create the request to the url passed in the paramaters
            request = (WebRequest)WebRequest.Create(url);


            // set the method to POST
            request.Method = "POST";

            // set the content type and the content length
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postdata.Length;

            // convert the post data into a byte array
            byte[] byteData = Encoding.UTF8.GetBytes(postdata);

            // get the request stream and write the data to it
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteData, 0, byteData.Length);
            dataStream.Close();

            // get the response
            response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);

            // read the response
            string serverresponse = reader.ReadToEnd();

            //Console.WriteLine(serverresponse);
            reader.Close();
            dataStream.Close();
            response.Close();

            return serverresponse;
        }
    } // end class httpPostData

...和我的呼唤

httpPostData myPost = new httpPostData();    
// postData defined (not shown)
string response = myPost.senddata("http://www.example.com/pdf.php", postData);

如果不清楚,我会坚持写入string response有效的 .pdf 文件。我已经尝试过了(感谢用户 Adrian):

static public void SaveStreamToFile(string fileFullPath, Stream stream)
    {
        if (stream.Length == 0) return;

        // Create a FileStream object to write a stream to a file
        using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
        {
            // Fill the bytes[] array with the stream data
            byte[] bytesInStream = new byte[stream.Length];
            stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

            // Use FileStream object to write to the specified file
            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
        }
    }

..and the call to it:

    string location = "C:\\myLocation\\";

    SaveStreamToFile(location, response);  // <<-- this throws an error b/c 'response' is a string, not a stream. New to C# and having some basic issues with things like this

我想我已经接近了......在正确的方向轻推将不胜感激。

4

1 回答 1

0

您可以使用WebClient。使用方法DownloadFile或异步方法。

玩得开心!费尔南多.-


抱歉,直到现在我还没有阅读您的评论。我想你已经这样做了......

但这可能会对您有所帮助(只需替换 url 和路径)(来自:http: //msdn.microsoft.com/en-us/library/ez801hhe.aspx

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;

Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);     

Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);

Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
于 2013-04-18T13:19:28.660 回答