Why does web browser makes two requests to download a mp3 file? - c#

I am creating a very simple web server where its only purpose is to return an mp3 file.
// create server
TcpListener server = new TcpListener(8585);
server.Start();
while (true)
{
// accept a new client
using (var client = server.AcceptTcpClient())
{
byte[] buffer = new byte[1024 * 2];
var i = client.Client.Receive(buffer);
// get request client sent us
var request = System.Text.Encoding.UTF8.GetString(buffer, 0, i).ToLower();
// if the browser is requesting an icon return
if (request.Contains("/favicon.ico"))
{
continue;
}
// with fiddler when I downloaded great.mp3 I saved the response as a binary file to make sure I am returning the right output
client.Client.Send(System.IO.File.ReadAllBytes("cheat.bin"));
}
}
cheat.bin can be downloaded from here
it basically consists of
HTTP/1.1 200 OK
Server: Apache/2.2.3 (CentOS)
X-HOST: sipl-web-233.oddcast.com
X-TTSCache: HIT
X-TTSCacheInsert: NO
Content-Type: audio/mpeg
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
Date: Thu, 01 May 2014 00:02:15 GMT
Content-Length: 5471
Connection: keep-alive
ID3���� .... rest of mp3 file!
So my question is why is it that when I go to http://localhost:8585/getSong on my web browser the song is downloaded twice? In other words if I place a breakpoint in my code I hit it twice. Also I will not be able to play the audio in the browser until I return the song a second time.
I am asking this question primarily to learn. I dont understand what is wrong.

Browsers often make a HTTP head call before a HTTP get don't they? Could that be why?
https://ochronus.com/http-head-request-good-uses/
Anyway I would use something like Fiddler to see exactly what HTTP requests the browser is making.

Related

eBay FileTransferService - Please specify a file with a valid format

I'm working with the eBay Large Merchant Services. I have successfully gotten the basic AddFixedPriceItem job to work using .NET HttpClient (finally). However, depending on the content of my AddFixedPriceItem XML file, the response to the uploadFile job would return:
Please specify a File with Valid Format
I'm not able to pinpoint what is causing this error.
Example #1:
I have a file with 2 AddFixedPriceItemRequest requests, which would go up successfully, and a file with 3 AddFixedPriceItemRequest requests which would not. When I would remove some NameValueList tags from the 3rd request, it would work. I don't see anything wrong or different with those NameValueList tags that I removed. If I send up the 3rd request by itself, without removing any tags it works perfectly.
Example #2:
I have 1 AddFixedPriceItemRequest request in a file that contains CDATA information for the Description tag (for HTML values), which would go up successfully, and another file with 2 AddFixedPriceItemRequest requests also containing almost identical CDATA values but would not work. If I remove the CDATA from the added second request, it still does not work. If I also remove the CDATA from the 1st request as well ( which went up successfully by itself the first time around) then it does work.
I'm really confused by this. There seems to be no rhyme and reason as to which a file is considered "valid".
Below is an example of a request that failed: (Stack Overflow doesn't seem to be taking the Content-IDs - but they are there.)
POST http://storage.sandbox.ebay.com/FileTransferService HTTP/1.1
X-EBAY-SOA-SECURITY-TOKEN: xxx
X-EBAY-SOA-SERVICE-NAME: FileTransferService
X-EBAY-SOA-SERVICE-VERSION: 1.0.0
X-EBAY-SOA-OPERATION-NAME: uploadFile
X-EBAY-SOA-OPERATION-FORMAT: XML
Content-Type: multipart/related; boundary="MIME_boundary"; type="application/xop+xml"; start="<0.urn:uuid:9ce221c2-659d-4852-a166-51dcbccf68d9>"; start-info="text/xml"
Host: storage.sandbox.ebay.com
Content-Length: 3041
Expect: 100-continue
--MIME_boundary
Content-Type: application/xop+xml
Content-ID: <0.urn:uuid:9ce221c2-659d-4852-a166-51dcbccf68d9>
Content-Transfer-Encoding: binary
<?xml version="1.0" encoding="utf-8"?><uploadFileRequest xmlns="http://www.ebay.com/marketplace/services"><fileAttachment><Data><xop:Include href="cid:urn:uuid:c6f7fc4d-352b-414f-8638-6c9bdf40519e" xmlns:xop="http://www.w3.org/2004/08/xop/include"/></Data><Size>1710</Size></fileAttachment><fileFormat>zip</fileFormat><fileReferenceId>50008675134</fileReferenceId><taskReferenceId>50008489744</taskReferenceId></uploadFileRequest>
--MIME_boundary
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-ID: <urn:uuid:c6f7fc4d-352b-414f-8638-6c9bdf40519e>
UEsDBBQAAAAIAMZZ9UhnranV5gUAADYmAAAPACQAYWRkbGlzdGluZ3MueG1sCgAgAAAAAAABABgA4roeimLj0QE1N5fd4t3RAex2PDlG3dEB7Vptc9o4EP4rmny59kOxDZiXjO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AA
--MIME_boundary--
Here's how I send the request with HttpClient:
byte[] data;
long fileLength;
var uuidRequest = Guid.NewGuid().ToString();
var uuidAttachment = Guid.NewGuid().ToString();
using (FileStream fs = File.OpenRead(filePath))
{
data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fileLength = fs.Length;
}
var xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<uploadFileRequest xmlns=\"http://www.ebay.com/marketplace/services\">" +
"<fileAttachment>" +
"<Data><xop:Include href=\"cid:urn:uuid:" + uuidAttachment + "\" xmlns:xop=\"http://www.w3.org/2004/08/xop/include\"/></Data>" +
"<Size>" + fileLength + "</Size>" +
"</fileAttachment>" +
"<fileFormat>zip</fileFormat>" +
"<fileReferenceId>" + fileReferenceID + "</fileReferenceId>" +
"<taskReferenceId>" + jobID + "</taskReferenceId>" +
"</uploadFileRequest>";
var multippartcontent = new MultipartContent("related", "MIME_boundary");
multippartcontent.Headers.Remove("Content-Type");
multippartcontent.Headers.TryAddWithoutValidation("Content-Type", string.Format("multipart/related; boundary=\"MIME_boundary\"; type=\"application/xop+xml\";start=\"<0.urn:uuid:{0}>\";start-info=\"text/xml\"", uuidRequest));
var content = new StringContent(xml);
content.Headers.ContentType = new MediaTypeHeaderValue("application/xop+xml");
content.Headers.Add("Content-ID", string.Format("<0.urn:uuid:{0}>", uuidRequest));
content.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary");
multippartcontent.Add(content);
var attachmentContent = new StringContent(Convert.ToBase64String(data));
attachmentContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
attachmentContent.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "base64");
attachmentContent.Headers.Add("Content-ID", string.Format("<urn:uuid:{0}>", uuidAttachment));
multippartcontent.Add(attachmentContent);
var uploadFileResp = httpclient.PostAsync(ftServiceUrl, multippartcontent).Result.Content.ReadAsStringAsync().Result;
I would note, when I upload this same file that doesn't work for me with the LMS project from eBay (https://ebaydts.com/eBayKBDetails?KBid=1338). That leads me me to think I'm doing something wrong in my code - perhaps something to do with how I'm encoding?
HTTP Response
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-EBAY-SOA-SERVICE-METRICS: 455075973
X-EBAY-SOA-REQUEST-ID: 15613449-f630-a471-d2d4-ec64ffff71c7!FileTransferService!10.71.29.45!v3apibulksandboxcore[]
X-EBAY-SOA-SERVICE-VERSION: 1.1.0
X-EBAY-SOA-MESSAGE-PROTOCOL: NONE
X-EBAY-SOA-RESPONSE-DATA-FORMAT: XML
X-EBAY-SOA-OPERATION-NAME: uploadFile
X-EBAY-SOA-SERVICE-NAME: {http://www.ebay.com/marketplace/services}FileTransferService
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 22 Jul 2016 15:41:19 GMT
1cd
<?xml version='1.0' encoding='UTF-8'?><uploadFileResponse xmlns="http://www.ebay.com/marketplace/services"><ack>Failure</ack><errorMessage><error><errorId>11</errorId><domain>Marketplace</domain><severity>Error</severity><category>Application</category><message>Please specify a File with Valid Format</message><subdomain>FileTransfer</subdomain></error></errorMessage><version>1.1.0</version><timestamp>2016-07-22T15:41:20.118Z</timestamp></uploadFileResponse>
0
I finally got the answers. The "100 continue" message gave me the needed clues. I was spending too much time trying to understand the intermittent operation.
1) Your code never really works. When you run the LMS project a cookie is establish which is setting up default setting for you request. So your code will always work after running the LMS. If you delete cookies from your IE history your code will never work.
2) The "100 continue message" is used for "Chunk Mode" (not stream). The Net library defaults to HTTP 1.1 and most server these days when the see 1.1 will default to chunk mode. The server sends one chunk , but there is no way in the Net library to request a 2nd chunk from server. So the connection times out and fails. The only way around this issue is to set the HTTP request to HTTP 1.0 which doesn't support chunk mode the server will send the response back as a stream.
Try this
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");
request.ProtocolVersion = HttpVersion.Version10;
See also : Set HTTP protocol version in HttpClient
Look at 1st line of request that failed which has 1.1

Fetching AccessToken returns "400 - Bad Request" for authenticating cloud service with WNS [duplicate]

PLEASE HELP!! Can't figure out why this simple code given by MSDN doesn't work....
I am using the following code in GetAccessToken() as given in the this MSDN article to get the access token to be used in windows notifications, but it returns "Bad Request 400"
PACKAGE_SECURITY_IDENTIFIER, CLIENT_SECRET are the values obtained when the app was registered with the Windows Store Dashboard
string urlEncodedSid = HttpUtility.UrlEncode(PACKAGE_SECURITY_IDENTIFIER);
string urlEncodedSecret = HttpUtility.UrlEncode(CLIENT_SECRET);
string body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlEncodedSid, urlEncodedSecret);
string response;
using (WebClient client = new WebClient())
{
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
response = client.UploadString("https://login.live.com/accesstoken.srf", body);
}
Any help would be highly appreciated.......
I suspect the problem has to do with either an incorrect package identifier, and / or incorrect client secret.
From the MSDN page Push notification service request and response headers:
RESPONSE DESCRIPTION
--------------- --------------------------
200 OK The request was successful.
400 Bad Request The authentication failed.
Update - I ran the code from the question, using FAKE credentials.
Here is the RAW HTTP request:
POST https://login.live.com/accesstoken.srf HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: login.live.com
Content-Length: 88
Expect: 100-continue
Connection: Keep-Alive
grant_type=client_credentials&client_id=test&client_secret=test&scope=notify.windows.com
Here is the server's RAW response:
HTTP/1.1 400 Bad Request
Cache-Control: no-store
Content-Length: 66
Content-Type: application/json
Server: Microsoft-IIS/7.5
X-WLID-Error: 0x80045A78
PPServer: PPV: 30 H: BAYIDSLGN2A055 V: 0
Date: Thu, 21 Mar 2013 12:34:19 GMT
Connection: close
{"error":"invalid_client","error_description":"Invalid client id"}
You will note that the response is a 400. There is also some json that indicates the type of error. In my case, the error is Invalid client id. You probably want to take a look at your response - it will give you an indication of what happened.
I used Fiddler to debug the request/ response.
I found the reason for the error response. In fact it is the wrong PACKAGE_SECURITY_IDENTIFIER and CLIENT_SECRET.
DO NOT type the values. Because associated ASCII values differ. Therefore it is always better to copy and paste directly.
You will probably will get the access token with the simple code snippet.
Cheers
If you're using the new HttpClient API and you're sure you've copied and pasted the SID/secret values correct, you might be experiencing this issue because of encoding, provided you're using the FormUrlEncodedContent class as the content of your POST operation.
Contrary to the examples in the MSDN documentation, you don't want to URL encode the SID and secret values before adding them to the KeyValuePair collection. This is because encoding is implied by the FormUrlEncodedContent class, though I'm not seeing any documentation for this behavior. Hopefully this saves someone some time because I've been wrestling with this all night...

Use Socket for received File in windows phone

I read the msdn tutorial for using socket with Windows phone. (http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=VS.96).aspx) I arrived to connect me to the server.
This, return me an response 200 ( OK response ) =>
HTTP/1.1 200 OK
Date: Mon, 16 Apr 2012 12:45:46 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny13 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g
Last-Modified: Tue, 08 Aug 2006 14:12:09 GMT
ETag: "129a2e5-100000-41a822e23a040"
Accept-Ranges: byte
But I don't know what to do next...
I connect me to the server, I sent this a "GET Message" for download a file
"GET /dwn/file.txt HTTP/1.1\r\nHost:myHost.com\r\n\r\n"
and After the response ? I have to do what?
If you know a tutorial, sample, or just explication, I would thank you greatly!
this my function =>
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
response = response.Trim('\0');
// show the response server value.
Debug.WriteLine(response.ToString());
// Data has now been sent and received from the server.
// Disconnect from the server
socket = e.UserToken as Socket;
socket.Shutdown(SocketShutdown.Send);
socket.Close();
clientDone.Set();
}
else
{
// show error in the debugger.
response = e.SocketError.ToString();
Debug.WriteLine(response);
}
}
Why don't you use WebClient or something like that ?
If it's just for experiencing, you should know that this is not a trivial task. Here are some hints:
You will probably not receive the whole HTTP response header + body in one "Receive" call. Remember, a TCP socket is a stream.
HTTP Headers are encoded in 7 bit ASCII, not UTF8.
HTTP Entity (well, body) encoding depends on headers, and decoding it can be tricky.
Have fun !
Edit
Clarifications (point #1):
// Data has now been sent and received from the server.
// Disconnect from the server
socket = e.UserToken as Socket;
socket.Shutdown(SocketShutdown.Send);
socket.Close();
Why do you shutdown and close the socket now? The response body follows the header in the stream. As I said, there is probably more bytes waiting to be received from the server. Continue to call "receive" until either:
There is no more bytes to read (socket is closed by the server)
You are sure you've read the whole response body (see "Content-Length" or "Transfer-Encoding" headers)

How to parse a raw HTTP response as HttpListenerResponse?

If I have a raw HTTP response as a string:
HTTP/1.1 200 OK
Date: Tue, 11 May 2010 07:28:30 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Server: gws
X-XSS-Protection: 1; mode=block
Connection: close
<!doctype html><html>...</html>
Is there an easy way I can parse it into an HttpListenerResponse object? Or at least some kind .NET object so I don't have to work with raw responses.
What I'm doing currently is extracting the header key/value pairs and setting them on the HttpListenerResponse. But some headers can't be set, and then I have to cut out the body of the response and write it to the OutputStream. But the body could be gzipped, or it could be an image, which I can't get to work yet. And some responses contain random characters everywhere, which looks like an encoding problem. It's a lot of trouble.
I'm getting a raw response because I'm using SOCKS to send an HTTP request. The program I'm working on is basically an HTTP proxy that can route requests through a SOCKS proxy, like Privoxy does.
Looks like there's no easy way to convert them. These articles helped:
How to implement custom proxy server?
http://www.jeffcrossman.com/2009/08/27/using-httplistener-to-build-a-proxy
I ended up doing something very similar.
Maybe you want to take a look at the ResponseHeaders property of the Webclient:
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://www.foo.com"));
WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders;
for (int i=0; i < myWebHeaderCollection.Count; i++)
Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) +
" = " + myWebHeaderCollection.Get(i));
Please tell me if that isnt what you were searching for.

How to simulate browser HTTP POST request and capture result in C#

Lets say we have a web page with a search input form, which submits data to server via HTTP GET. So that's mean server receive search data through query strings. User can see the URL and can also initialize this request by himself (via URL + Query strings).
We all know that. Here is the question.
What if this web page submits data to the server via HTTP POST? How can user initialize this request by himself?
Well I know how to capture HTTP POST (that's why network sniffers are for), but how can I simulate this HTTP POST request by myself in a C# code?
You could take a look at the WebClient class. It allows you to post data to an arbitrary url:
using (var client = new WebClient())
{
var dataToPost = Encoding.Default.GetBytes("param1=value1&param2=value2");
var result = client.UploadData("http://example.com", "POST", dataToPost);
// do something with the result
}
Will generate the following request:
POST / HTTP/1.1
Host: example.com
Content-Length: 27
Expect: 100-continue
Connection: Keep-Alive
param1=value1&param2=value2

Categories