How to compress ASMX Data With String JSON Response(C# Web Service) - c#

I'm calling my webservice from an Android application one after another. And at every call it returns with almost 3.5KB data.
I'm using JavascriptSerializer class to serialize and convert to JSON string my Dictionary<string,string> or Dictionary<string,string>[] objects. (Especially Dictionary<string,string>[])
Is there a way to reduce this amount of data. It's so much.Or am i doing something wrong?
Thanks..

Script files loaded via an HTML element within a browser can only be retrieved via HTTP GET verb requests.
By default ASP.NET AJAX's web services layer does not allow web methods to be invoked via the HTTP GET verb. For example, assume a developer writes a web service method like below:
[WebMethod]
public StockQuote[] GetQuotes(string symbol) {
}
ASP.NET will only allow the above GetQuotes method to be called via the HTTP POST verb, and will reject all attempts to invoke the method via an HTTP GET verb.
To make an ASP.NET AJAX web-method callable via HTTP GET-access, a developer must explicitly attribute each method using ASP.NET's ScriptMethod attribute (and set the UseHttpGet property to true):
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public StockQuote[] GetQuotes(string symbol) {
}
for more info please refer below link
http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx

Related

call a regular MVC controller method from a webapi2 service

Given an MVC controller method, that builds and returns a PDF.
[HttpGet]
[Route("pdf/{acc}/{sign}")]
public async Task<ActionResult> Download(string acc, string sign)
{
... // omitted some irrelevant detail.
var html = this.View("Letter", model).Capture(this.ControllerContext);
byte[] pdf = this.CreatePdfFromHtml(html);
return this.File(binary, "application/pdf", file);
}
The Capture extension method captures the html output, which is then returned as a file.
I need to execute the above method (or something close) in the below webapi2 method.
I need the credentials and other login state to be present. The webapi and MVC requests are within the same application that uses cookies for security.
[HttpPost]
[Route("generate")]
public int Generate(MyRequestModel request)
{
byte[] pdf = ... // how do i get the file above??
}
You should either request the MVC action via HttpClient or simply factor out the PDF creation into a helper class that both your MVC and Web Api actions can reference.
With the first method, you would need to some how authenticate the request, which is going to be a bit difficult to do with an MVC action. MVC uses a multi-step authorization: sign in, verify, set cookie, redirect. You would have to follow those same steps in order to get a cookie set via HttpClient. Think of it as a little mini-browser. Web Api, on the other hand, simply accepts an Authorization header, since API requests are idempotent.
The easiest and most straight-forward route, especially since both your MVC and Web Api reside in the same application, would be to simply factor out the PDF creation code into a helper class. Your MVC and Web Api actions, then, can simply just call some method on that class to get the PDF, reducing code duplication.

How to call a Rest Webservice and return json string in c#.net

I created a Rest Webservice with WebApi. This have 4 HTTP verbs. That is Get,Post,Put,Delete.
I want to call all these methods in webservice in C# window form application. It is not possible to add service reference for this Webservcie. How could i call GET,Post,put and delete method? I want to pass json string with POST request. Please help
If you just want to call the service and pass along the POST data manually, and also manually parse the results, you can use System.Net.Http.HttpClient.

c# app: Is it possible to implement a JSON interface?

I've built a simple C# app (.Net 4.0 + WPF) which can send and receive JSON messages via TCP sockets.
As a next step, it should be possible that JavaScript apps on websites and PHP scripts can send and receive JSON messages to/from my app. Is that possible?
Since JS/PHP will use stateless HTTP connections, how should a request to my app work, for example, should the JS/PHP apps send a JSON message to my app and my app response (HTTP response) with a JSON message? Is that even possible? And should I use GET or POST method to send the JSON messages to/from my app?
Hope my questions do not cause too much confusion ;-) I but I appreciate every tip, clarification or feedback you can give me.
Mike
You can accomplish this via a .NET web service using special JSON directives on the web method, e.g.
[ScriptMethod(UseHttpGet = true, ResponseFormat=ResponseFormat.Json)]
public string DoSomething(string param1, int param2)
{
// Do Something
}
When the ResponseFormat.Json property is specified, the data returned will be serialized into the appropriate JSON format. Also note, in order to recieve a true JSON response, you'll need to set your content-type to "application/json" from the requesting application. Otherwise, the method will attempt to wrap the response in XML.
Also, I am enabling a HttpGet on this method so that you can post via a query string to the method, e.g.
http://www.example.com/service.asmx?param1='Hello'&param2=1;

How to receive XML in ASP.NET Web Service?

I have a quasi-RESTful ASP.NET web service that I wish to be able to post XML to. My method looks something like this:
[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)]
public void save(string saveXml)
{
XDocument saveXml = XDocument.Parse(saveXml);
....
When I try to post to that web service, I get the exception:
System.Web.HttpRequestValidationException: A potentially dangerous Request.Form
value was detected from the client...
Based on some searching, my understanding is that all requests to ASP.NET pages look for characters like "<" which are deemed dangerous (obviously this applies to my XML). This can be turned off easily for individual pages, but not web services. The only solution I have found involved changing the request validation mode in my web.config to "2.0". I don't want to change to an old version of something just because my one web service method doesn't work with the 4.0 version. Is there any way to disable this for my specific method? Thanks in advance.
The new 4.0 RequestValidation model is a bit pickier, but as long as your client sends the HTTP request Content-type header as text/xml instead of application/x-www-form-urlencoded it should pass (or bypass rather) ASP.NET RequestValidation.

How can i return raw bytes from ASP.NET web service?

If the WebMethod returns string it gets serialized to xml.
I want to return byte[] with responding ContentType - can I also specify it?.
Can it be done in ASP.NET web service web method?
ASMX web services use SOAP and the content type will always be application/soap+xml and the content represent xml. Even if you return a byte[] from your method this array will be base64 encoded into the soap body.
You can return a byte array from a web service, but it will still be serialised into the response message. (Typically as base-64 in a SOAP XML response.)
If you want to return only the binary content you shouldn't use a web service. Instead you can use Response.BinaryWrite with a regular page with no html content, or context.BinaryWrite in a http handler.
Instead of a web service, use a Generic Handler (.ashx).
A Generic Handler accepts get/post requests and gives you the ability to completely control the output via the HttpContext.
I typically use these for sending files (pdfs, etc) to the browser.

Categories