Windows Phone 7.1 WebClient POST without body - windows-phone-7

I am new to Web service calls; I can do simple GET and parse the JSON output. Not good with POST and especially if the URI has spaces and quotes.
I am trying to send the following URI to the server in Windows Phone 7.1 (Emulator). It says "Not found error". This error seems to be generic; it doesn't say what's really was wrong anyway.
https://aaabbb.com//services/v4/put/users/xxxxx/device?deviceId=2NDJDRkI5MEVFME -H 'Access: Token token=CXJTY'
There is no JSON body/data for this. It just this URL with the shown params. thats where data is too.
how do I do this with WebClient class or RestSharp? I tried this using WebClient class in WP 7. didn't succeed. So downloaded RestSharp; but not sure how the above URI without any JSON data could be sent.
I followed some of the posts in here to use the WeClient class. It didn't work.

You need to URL-encode your string. For that, you can do something like this:
string deviceId = HttpUtility.UrlEncode("2NDJDRkI5MEVFME -H 'Access: Token token=CXJTY'");
Uri uri = new Uri("https://aaabbb.com/services/v4/put/users/xxxxx/device");
string data = "deviceId=" + deviceID;
WebClient wc = new WebClient();
wc.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded";
wc.UploadStringAsync(uri, data);
wc.UploadStringCompleted += wc_UploadComplete;

Related

WebRequest to Shoutcast throws "invalid or unrecognized response" exception

I'm trying to read metadata from shoutcast stream using ASP.net WebAPI, .net core 2.1. All I need is the headers and not the audio data.
I found out that Shoutcast servers 2+ give stats xml page, but for compatibility reasons, I need to work this out so I can support v1 too. The /7.html does not give the title and genre.
Following is piece of relevant code:
HttpWebRequest request = null;
HttpWebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(server);
request.Headers.Clear();
request.Headers.Add("GET", "/ HTTP/1.0");
// needed to receive metadata information
request.Headers.Add("Icy-MetaData", "1");
request.UserAgent = "WinampMPEG/5.09";
response = (HttpWebResponse)request.GetResponse();
info.Title=response.Headers["icy-name"];
info.Genre=response.Headers["icy-genre"];
When I run this code on IIS Express, or publish and run on IIS, I get this error:
An unhandled exception occurred while processing the request.
HttpRequestException: The server returned an invalid or unrecognized response.
System.Net.Http.HttpConnection.ThrowInvalidHttpResponse()
WebException: The server returned an invalid or unrecognized response.
System.Net.HttpWebRequest.GetResponse()
I have tried WebClient and StreamReader as well but the issue seems to be consistent.
However I tried the same code in Console Application and it seems to work just fine.
How can I get this to work through a WebAPI on IIS?
I had same issue. I used HttpClient and PostAsync to fix the issue.
It is to note that I initially tried my code with dot net framework rather than dot net core and it was working fine, so I think HttpWebResponse is not compatible with dot net core in some cases.
Have a look on below links, if using HttpClient still does not fix your issue. I hope it helps.
https://github.com/dotnet/corefx/issues/14897
https://github.com/dotnet/corefx/issues/30040

DownloadString works for HTTP but not for HTTPS

I have webclient call in my SSIS package which call an API to get JSON response.
using (var mySSISWebClient = new System.Net.WebClient())
{
mySSISWebClient.Headers[HttpRequestHeader.Accept] = "application/json";
var result = mySSISWebClient.DownloadString(jsonURLwithDate);
}
When I change the URL to use HTTPS, the package is failing at downloadstring call with the following error.
Download failed: The underlying connection was closed: An unexpected error occurred on a send.
It is working fine when I have the URL with HTTP. I am working on it in SSDT 2010. Please help me to resolve this.
Thanks
I think you might need to add the following code to implement TLS1.2:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
based on this article
https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/

How to forward to an external url?

My Spring based app is running under http://localhost. Another app is running under http://localhost:88. I need to achieve the following: when a user opens http://localhost/page, a content of http://localhost:88/content should be shown.
I've supposed, that I should use forwarding, like shown bellow:
#RequestMapping("/page")
public String handleUriPage() {
return "forward:http://localhost:88/content";
}
but seems like forwarding to an external URL doesn't work.
How can I achieve this behaviour with Spring?
Firstly, you specify that you want to show the content of "http://localhost:88/content" but you actually forward to "http://localhost:88" in your method.
Nevertheless, forward works with relative URLs only (served by other controllers of the same application), so you should use 'redirect:' instead.
Forward happens entirely on the server side: the Servlet container forwards the same request to the target URL, so the URL won't change in the address bar.
Redirect, on the other hand, will cause the server to respond with 302 and the Location header set to the new URL, after which the client browser will make a separate request to it, changing the URL in the address bar, of course.
UPDATE: For returning the content of the external page as it would be an internal one, I would write a separate controller method to make the request to the URL and just return its content. Something like the following:
#RequestMapping(value = "/external", produces = MediaType.TEXT_HTML_VALUE)
public void getExternalPage(#RequestParam("url") String url, HttpServletResponse response) throws IOException {
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(url);
HttpResponse response1 = client.execute(request);
response.setContentType("text/html");
ByteStreams.copy(response1.getEntity().getContent(), response.getOutputStream());
}
Of course, you have many possible solutions. Here I used Apache Commons HttpClient for making the request, and Google's Guava for copying the response from that request to the resulting one.
After that, your return statement would change to the following:
return "forward:/external?url=http%3A%2F%2Flocalhost%3A88%2Fcontent"
Note how you need to encode your URL given as parameter.

Using WebClient.UploadStringAsync with GET data

I'm trying to use WebClient.UploadStringAsync method to send some data to server. It works fine when I send POST data, but when using GET, it throws me an error "An exception occurred during a WebClient request."
Here is my code:
WebClient client = new WebClient();
String data = "param1=value1&param2=value2";
client.UploadStringAsync(new Uri("http://somesite.com"), "GET", data);
Any idea what's going wrong?
Don't use UploadStringAsync for GET. There is DownloadStringAsync designed specifically for that.
Don't use WebClient because it is bound to the UI thread. Use HttpWebRequest instead.
Uploading data for a GET breaks convention.
You might also want to take a look at HTTPClient which you can install via NuGet.

Windows phone 7 - How to use HTTPWebRequest to POST / GET data from a .jsp site[with cookies]

Title sums it up fairly well.
Said site has cookies, I need to post data from a textbox as a value on said site, and get one of two variables back. I was reading through some tutorials and a few Windows phone 7 books. None of them were related to what I was trying to do. They only dealt with single whole files or something that could be made into a URL. I could also do it that way if someone had a way to also use cookies and just send it as a url [but i do not know how to construct the url in such a way to make that a realistic solution].
You should be able to send cookie's using code like:
CookieContainer container = new CookieContainer();
container.Add(new Uri("http://yoursite"), new Cookie("name", "value"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://yoursite");
request.CookieContainer = container;
request.BeginGetResponse(new AsyncCallback(GetData), request);
Code borrowed from HttpWebRequest and Set-Cookie header in response not parsed (WP7)
If the server is expecting to use cookies (e.g. for authentication) then there is no way that you'll be able to use form variables/query parameters instead.
You need to use a tool called Fiddler to inspect the calls that the website currently makes - this will include a mixture of:
cookie variables - especially for authentication
get variables - passed within the url path
and post variables - passed within the body of the request
If you do need to do a full POST, then you will need to set variables like - request.Method and request.ContentType - and you will need to brovide a RequestStream. There are libraries you can use like HAMMOCK to help - or I've got some example code in iron7 - see the DoCodePost method at the botom of this uploader class - or take a look at lots of other projects on CodePlex and GitHub.

Resources