Read and Download Response from URL using C# .Net

Last Reply on Oct 07, 2015 06:54 AM By Shashikant

Posted on Oct 06, 2015 02:14 AM

This URL A :

https://microsoft-apiapp80ce5187f0b2490aadbc980673a5485d.azurewebsites.net/api/GetBlobs/Blob?blobContainer=12062&blobName=12061259.pdf&passKey=RecorderAz4ass3ey

generates into URL B:

https://recorderdocs.blob.core.windows.net/12062/12061259.pdf?sv=2015-04-05&sr=b&sig=h9md7uWLdGuzxCGIvXr4doYOm3GFyK7jw3xQ8plD%2B4Q%3D&st=2015-10-06T01%3A03%3A20Z&se=2015-10-06T01%3A28%3A20Z&sp=r

You can try it by copying and paste the URL Link A  on your browser and  you will see the URL Link  A  generates the URL Link B, afterwards I will need to copy and paste mannually  the URL Link B,  which will display an image file for your view.  This will give you an idea what I am trying to do.

I would like to be able in the code-behind clicking the button from URL A to URL B to display an image in the popup window. How to do this process in the code-behind?

See the code below:

 

Protected Sub BtnSAS_Click(sender As Object, e As EventArgs) Handles BtnSAS.Click

Dim IdNumber As String = TextNumber.Text.Trim()

Dim UrlA as string =”https://microsoft-apiapp80ce5187f0b2490aadbc980673a5485d.azurewebsites.net/api/GetBlobs/Blob?blobContainer=12062&blobName=” & IDNumber & ”.pdf&passKey=RecorderAz4ass3ey”

'generate into URL B

ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Key", "NewWindow=window.open('https://recorderdocs.blob.core.windows.net/12062/12061259.pdf?sv=2015-04-05&sr=b&sig=h9md7uWLdGuzxCGIvXr4doYOm3GFyK7jw3xQ8plD%2B4Q%3D&st=2015-10-06T01%3A03%3A20Z&se=2015-10-06T01%3A28%3A20Z&sp=r','PDFViewer','menubar=0, location=1, toolbars=1, scrollbars= 1, resizable=1, width=750,height=775, top=150, left=220')", True)

End Sub

Your help is greatly appreciated.  Thanks.

Posted on Oct 07, 2015 06:54 AM

Hi slb2013,

Here I have created sample that full-fill your requirement.

HTML

<div>
    Enter Id Number : <asp:TextBox runat="server" ID="TextNumber"></asp:TextBox>&nbsp;&nbsp;
    <asp:Button ID="btnSAS" Text="SAS" runat="server" OnClick="BtnSAS_Click" />
</div>

C#

protected void BtnSAS_Click(object sender, EventArgs e)
{
    string IdNumber = TextNumber.Text.Trim();
    string UrlA = "https://microsoft-apiapp80ce5187f0b2490aadbc980673a5485d.azurewebsites.net/api/GetBlobs/Blob?blobContainer=12062&blobName=" + IdNumber + ".pdf&passKey=RecorderAz4ass3ey";
    string UrlB = ResFromServer(UrlA);
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key", "NewWindow=window.open('" + UrlB + "','PDFViewer','menubar=0, location=1, toolbars=1, scrollbars= 1, resizable=1, width=750,height=775, top=150, left=220')", true);
}

public string ResFromServer(string url)
{
    string location = string.Copy(url);
    string responseFromServer = string.Empty;
    if (!string.IsNullOrEmpty(location))
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location);
        request.AllowAutoRedirect = false;
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            Stream dataStream;
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            responseFromServer = reader.ReadToEnd();
        }
    }
    return responseFromServer;
}

Screenshot