I am trying to achieve this:
I have a website from where users buy files and then they see the download links.
Files are located on another location (some www.myfiles.com) the links are secure so the users dont see where are the files but actually the browser should start downloading the files as soon as they click.
user buy files, click on the link and i do this:
var filename = "SomeHighlySecureFile.mp3";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myfiles.com/download.aspx?file=" + filename);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + fileinfo.Name);
context.Response.ContentType = "application/octet-stream";
I have no idea, what to do next? coz the "WriteFile" does not provide the options to write another stream?
Can anyone give me a clue how to do that?
Related
I thought I'd share something that took me some time to figure out:
I wrote a simple Post method using HttpWebRequest class.
In HttpWebRequest you can't use HttpWebRequest.Headers collection to set your desired headers when there is a dedicated property for it - you must use that dedicated property. ContentType is one of them. So I created my HttpWebRequest like this:
HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.ContentType = "application/json";
somewhere below I set the body of my request like this:
using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
{
streamWriter.Write(sJson);
}
and posted the request using:
WebResponse webResponse = httpWebRequest.GetResponse();
But I kept getting a "400 - Bad Request" error, while the same request worked from Postman. After analyzing the request with Fiddler I found that when I send the request from my app, the Content-Type: application/json header is missing. All the other headers were present, except for Content-Type. I thought I'm setting it wrong, so I googled but didn't find a good answer. After much experimentation I found, that if I move the line:
httpWebRequest.ContentType = "application/json"
after this block:
using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
{
streamWriter.Write(sJson);
}
then the httpWebRequest.ContentType = "application/json" header finally appears in the request. So, for HttpWebRequest make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.
Hope it helps
My question above already has the answer, but to mark it as "Answered" I had to add this comment:
Make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.This way the "Content-Type" header will appear in the request.
I need to post a request from JSON file in my selenium test. The below code is showing response code as 400.
Manually tested, it's working fine and response code is 200 for the same json body.
When this json body is stored in a json file and executed the below code, then response code displays as 400.
String fileName = "D:/json/test1.json";
String json = FileUtils.readFileToString(new File(fileName), "utf-8");
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); // required url is already saved in 'url' variable.
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/JSON");
con.setRequestProperty("Accept", "application/JSON");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
byte[] response = json.getBytes("utf-8");
out.write(response.toString());
System.out.println(httpConn.getResponseCode());
Please, appreciate any help on this?
How to convince chrome(browser) to show the download file dialog while downloading a JSON content?
With the following headers chrome renders the JSON directly on the screen. Content-Type:application/json; charset=utf-8
Any ideas?
As it turns out, you only have to set the content disposition on the server end,
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition: attachment; filename=" + fileName + ".json");
I need to be able to login to my gmail account, then i get cookies and will have access to other google services. But i can't login to my gmail(or any goolgle) account. I found some posts on this site how to do it, but none works for me. i do :
string formUrl = "https://www.google.com/accounts/ServiceLoginAuth";
string formParams = string.Format("Email={0}&Passwd={1}&signIn={2}&PersistentCookie={3}&GALX={4}",
"autokuzov.top", "1QAZ2wsx", "Sign in", "yes", "CfFosrEhu-0");
string cookieHeader;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Referer = "https://www.google.com/accounts/ServiceLoginAuth";
req.Method = "POST";
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7";
req.AllowAutoRedirect = false;
req.CookieContainer = new CookieContainer();
req.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie");
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string s = sr.ReadToEnd();
}
Response return : "Your browser's cookie functionality is turned off. Please turn it on."
I also tried make req.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie"); but it was unseccussfull too.
Does anybody know where is a problem ?
"Your browser's cookie functionality
is turned off. Please turn it on."
You will probably need to have 3rd party cookies enabled in your browser. These are off by default in some browsers. You get the same warning in Firefox when using the Gmail Manager plugin if you disable 3rd party cookies.
In VB.NET, how can I write a memory stream to browser. My memory stream object has data to build a PDF file. Now I want it to be rendered on browser. How to do that?
You could try something like:
Dim stream As MemoryStream = GetMemoryStream()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=yourfile.pdf")
Response.Write(stream.ToArray())
Response.End()
I have not tested the code nor am I sure of the mime type, but this should get you started.