The argument --path-as-is in CURL by definition sends the request with the URL path as is, without squashing (Do not squash .. sequences in URL path).
How can I achieve the same behavior in C# using either WebClient or WebRequest?
Related
What is the best way to deal with special characters in URL's with ServiceStack and a Javascript Axios client, or any other client.
Example:
URL Path: /MasterItems/{Code} - Code can have any character in it, (/ \ & etc.)
The above URL could be generated by the API (backend), so it will come back from a previous request as part of the response _links.
Example: of the response _links
/MasterItems/A1200G/FA (the code is A1200G/FA)
My frontend code, (VueJS, Javascript, Axios) will simply get the _links resource and call the GET.
How should I treat this, turn my GET into POST and pass a parameter?
Encode the URL?
Note:
I noticed when using the built-in swagger feature of ServiceStack the resulting call works.
The URL will be: /MasterItems/%7BCode%7D?Code=A1200G%2FA
I am trying to use the new Facebook Messenger Platform API to send image message using image file from application directory.
Facebook gives the example using cURL like below:
curl \
-F recipient='{"id":"USER_ID"}' \
-F message='{"attachment":{"type":"image", "payload":{}}}' \
-F filedata=#/tmp/testpng.png \
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
But I am trying to use the API with C#. For your information, I have successfully use the API if I use the file from an internet url.
I have tried to fill in the filedata property by using base64 string of the image file, but unsuccessful.
Kindly explain how does cURL works with the given file path especially image and create a POST request to the web server? And if possible, what options do I have to do it with C#?
The -F option is for form. This is equivalent to issuing a POST request with the Content-Type header of multipart/formdata and the request body containing all the key-value pairs listed with a proper boundary set. cURL will read the binary data and put the bytes in the correct boundary in the request. There are many examples online for C# to submit a multipart/formdata request. Look into HttpClient or WebClient file uploads and you'll find what you need.
I'll be away from a computer for a few days and submitting sample code from a mobile device isn't the easiest thing to do. If you need some sample code, let me know.
I am workign with OAuth2 and and I am calling Google API. Google returns results after call is complete and I am supposed to read from the query parameters. Now, the kind of URL that Google Returns is weird and it has anchor # in it exactly where there should be a ?
and URL looks something like
http://localhost.contestfactory.com/enduser/#state=MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw&access_token=ya29.AHES6ZTjWwx7hHO4WnmfQ_lwJSpATCqA_DUZCC_ZIjdyPWA96OS0EN0&token_type=Bearer&expires_in=3600
Because of # in the URL my C# code fails to read beyond #. Is there anyway I can deal this problem in C#?
The part after the # is called the URL fragment (or hash).
It is never sent to the server.
what ur saying looks like hash fragment.. and it is never sen tto the server. what u can do instead make a JS script that changes all the "#" to "?" that way they will be treated as query string and will be sent to the server
I am trying to send a request to an url like this "http://mysite.dk/tværs?test=æ" from an asp.net application, and I am having trouble getting the querystring to encode correctly. Or maybe the querystring is encoded correctly, the service I am connecting to just doesn't understand it correctly.
I have tried to send the request with different browsers and logging how they encode the request with Wireshark, and I get these results:
Firefox: http://mysite.dk/tv%C3%A6rs?test=%E6
Ie8: http://mysite.dk/tv%C3%A6rs?test=\xe6
Curl: http://mysite.dk/tv\xe6rs?test=\xe6
Both Firefox, IE and Curl receive the correct results from the service. Note that they encode the danish special character 'æ' differently in the querystring.
When I send the request from my asp.net application using HttpWebRequest, the URL gets encoded this way:
http://mysite.dk/tv%C3%A6rs?test=%C3%A6
It encodes the querystring the same way as the path part of the url. The remote service does not understand this encoding, so I don't get a correct answer.
For the record, 'æ' (U+00E6) is %E6 in ISO-LATIN-1, and %C3%A6 in UTF-8.
I could change the remote service to accept the UTF-8 encoded querystring, but then the service would stop working in browsers and I am not really interested in that. Is there a way to specify to .NET that it shouldn't encode querystrings with UTF-8?
I am creating the webrequest like this:
var req = WebRequest.Create("http://mysite.dk/tværs?test=æ") as HttpWebRequest;
But the problem seems to originate from System.Uri which is apparently used inside WebRequest.Create:
var uri = new Uri("http://mysite.dk/tværs?test=æ");
// now uri.AbsolutePath == "http://mysite.dk/tv%C3%A6rs?test=%C3%A6"
It looks like you're applying UrlEncode over the entire URL - this isn't correct, paths and query strings are encoded differently as you've seen. What is doing the encoding of the URI, WebRequest?
You could manually build the various parts using a UriBuilder, or manually encode using UrlPathEncode for the path and UrlEncode for the query string names and values.
Edit:
If the problem lies in the path, rather than the query string you could try turning on IRI support, via web.config
<configuration>
<uri>
<iriParsing enabled="true" />
</uri>
</configuration>
That should then leave international characters alone in the path.
Have you tried the UrlEncode?
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
I ended up changing my remote webservice to expect the querystring to be UTF-8 encoded. It solves my immediate problem, the webservice can not be correctly called by both PHP and the .NET framework.
However, the behavior is now strange in browsers. Copy pasting an url like "http://mysite.dk/tv%C3%A6rs?test=%C3%A6" into the browser and then pressing return works, it even corrects the encoded characters and displays the location as "http://mysite.dk/tværs?test=æ". If then reload the page (F5) it still works. But if I click on the location bar and press return again, the querystring will become encoded with latin-1 and fail.
For anyone interested here is an old Firefox bugreport about the problem: https://bugzilla.mozilla.org/show_bug.cgi?id=284474 (thanks to #dtb)
So, it seems there is no good solution.
Thanks to everyone who helped though!
i am new to servlets. I made a simple hello world programme that prints those famous 2 words to the screen when i access the page via a browser using http://localhost:8080/mypath/path...
My question is now, how do i provide a servlet with information? Specifically considering the fact that my c# programme calculates a lookup value for a global system, then the "java" servlet processes that and returns a set of results.
I have done the reverse thanks to help on here, regarding calling a returned string from the java servlet in c#. However i am not sure how i go about providing the servlet information. Do i pass it via the url?
Thank you
If you mean that you wish to pass information to servlet you can do it:
using request parameters (for example url arguments if you use GET method)
by parsing request URL (see HttpServletRequest.getRequestURL() and getRequestURI())
by examining the HTTP headers and cookies (see HttpServletRequest.getHeaders() )
If you are asking about sending information from servlet back to client you can write it into output stream got from HttpServletResponse.