Using WebClient.UploadStringAsync with GET data - windows-phone-7

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.

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/

Windows Phone 7.1 WebClient POST without body

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;

Retrieving JSON from an external API with backbone

I'm new to backbone.js and I've read other solutions to similar problems but still can't get my example to work. I have a basic rails api that is returning some JSON from the url below and I am trying to access in through a backbone.js front end. Since they are one different servers I think I need to use a 'jsonp' request. I'm currently doing this by overriding the sync function in my backbone collection.
Api url:
http://guarded-wave-4073.herokuapp.com/api/v1/plans.json
sync: function(method, model, options) {
options.timeout = 10000;
options.dataType = 'jsonp';
options.url = 'http://guarded-wave-4073.herokuapp.com/api/v1/plans.json'
return Backbone.sync(method, model, options);
}
To test this I create a new 'plans' collection in my chrome console using "plans = new Plans()" and then "plans.fetch()" to try and get the JSON.
When I call plans.models afterwards I still have an empty array and the object that returns from plans.fetch() doesn't seem to have any json data included.
Any ideas where I'm going wrong?
I have had the same problem before. You should not have to override your sync method.
Taken from Stackoverflow Answer
"The JSONP technique uses a completely different mechanism for issuing HTTP requests to a server and acting on the response. It requires cooperating code in the client page and on the server. The server must have a URL that responds to HTTP "GET" requests with a block of JSON wrapped in a function call. Thus, you can't just do JSONP transactions to any old server; it must be a server that explicitly provides the functionality."
Are you sure your server abides to the above? Test with another compatible jsonp service (Twitter) to see if you receive results?
Have you tried overriding the fetch method as well?
You should add ?callback=? to your api url in order to enable jsonp

Apache CXF Webservice failing on AJAX call

We have developed a webservice based on Apache CXF.
This is working fine when accessed normally that is using APIGEE or by using a JaxWsProxyFactoryBean (A clinet for Apache CXF). But when I tried to access this by providing the SOAP Address through AJAX call it is giving me the following exception:
INFO: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No such operation: (HTTP GET PATH_INFO: /tata-ws-1.0/TataWeb)
at org.apache.cxf.interceptor.URIMappingInterceptor.handleMessage(URIMappingInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:89)
...
This is my AJAX call code happens on click of a button
<a onclick="sendRequest('GET','http://localhost:8080/tata-ws-1.0/services/TataWeb')"href="#">
Get Data:
function sendRequest(method, url)
{
method == 'POST';
{
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}
The URL for WSDL is correct because when i use
http://localhost:8080/tata-ws-1.0/services/TataWeb?wsdl it shows the WSDL for that webservice.
Please share your inputs.
Thanks.
Your need to send a SOAP request rather than a regular HTTP request.
Best way would be use something like this:
http://archive.plugins.jquery.com/project/jqSOAPClient

Resources