Adding Headers to Soap Request .Net - asp.net

I added a web service reference to my existing visual studio project, I not able to execute any of the service methods as i keep getting "missing headers" error.
Then newly generated proxy class doesn't have method to add headers. Is there a way i can add http header to the request

Override the method GetWebRequest in the proxy class
Then add below code
HttpWebRequest request = base.GetWebRequest();
request.Headers.Add("HeaderName", "HeaderValue");
return request

Related

How to get Http header values in Apache Camel- Jersey Rest API

I have an application which uses Apache Camel to build an API. It basically uses blueprint.xml to define routes and processing is done by a bean(please note its not any processor bean. Just a plain Java bean). It uses Jersey client to invoke the backend system Rest API.
My requirement is to get the http headers in the code to be able to send them to our custom logging system.
a) I tried #httpHeaders annotation but this does not inject the headers on my code.
b) Since its not using any BeanProcessor i dont have an Exchange object from where i can get the header values.
Please help with a way to get header values on the code.
Add the request context to your class
#Context
private HttpServletRequest request;
and get the headers in your endpoint using request.getHeader
Returns the value of the specified request header as a String.

how to consume .svc file

I have got url for svc file. For example https://Myservicelocation/UserService.svc.
Now i tried to create asp.net web application in visual studio and added this url using "Add service reference". When i put this url and then click go i get error
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 'https://Myservicelocation/UserService.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service https://Myservicelocation/UserService.svc. The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
If the service is defined in the current solution, try building the solution and adding the service reference again.
How can i resolve this?
Google says..
Probably a mismatch in the client/server bindings, where the message version in the service uses SOAP 1.1 (which expects application/soap+xml) and the version in the client uses SOAP 1.2 (which sends text/xml). WSHttpBinding uses SOAP 1.2, BasicHttpBinding uses SOAP 1.1.
Align your bindings accordingly...
I had the same problem.
Try https://Myservicelocation/UserService.svc?wsdl link.

Can't get authentication token from web api 2

I am new to Web Api 2. I am trying to build a project to explore token authorization. I created a new project in VS 2013 and selected the WebApi2 template and used Fiddler to emulate http requests. I didn't change anything in the template, just ran it as it was and tried to play with it with Fiddler. I successfully created a user by issuing request to /api/account/register but I can't login by issuing a POST request to the /Token endpoint. The request is:
http://localhost:YYYY/token?grant_type=password&password=admin123456&username=admin
(i also tried to pass the parameters as a json object in the request body).
I get back this:
{"error":"unsupported_grant_type"}
From other posts such as ASP.NET WEB API 2 OWIN Authentication unsuported grant_Type I learned that I needed to enable CORS for web api and at the token endpoint, but that hasn't worked for me.
Are you sure that you are sending POST request message and not GET?
If you simply go to the URL with query string (or open connection to this URL from your code) you are sending GET message by default. It's not what WebAPI with "/token" path is listening for.
If you are calling web service from same place, CORS is not needed. The error "unsupported_grant_type" could be in the format of the data you are passing server in post action.
Try sending with Content-Type application/x-www-form-urlencoded

Return status code 4xx for client requests with invalid/unsupported Content-Type (like text/plain)

I've implemented a RESTful API using ASP.NET Web API (included in ASP.NET MVC 4).
When a client requests my services with an unsupported Content-Type (like "text/plain"), I get the following exception:
System.InvalidOperationException: No MediaTypeFormatter is available
to read an object of type [...] from content with media type
'text/plain'.
and my service returns a HTTP status code 500 and an error is logged/reported (I'm using ELMAH).
Unfortunately, I do not find an easy way to "catch" this type of unsupported client request. IMHO, the service should return a status code like 404 (not found) or 400 (bad request), because the error is not a server error (but a client side error).
In another SO question I found this answer How do I configure the status code returned by my ASP.NET Web API service when the request has an unsupported Content-Type? which shows a possible solution by implementing a custom DelegatingHandler.
So my question is: Do I miss something? Is there any ASP.NET Web API built-in functionality that targets this problem...
I finally "fixed" the problem with a custom DelegatingHandler similar to this implementation
How do I configure the status code returned by my ASP.NET Web API service when the request has an unsupported Content-Type?.
You can find my implementation here: http://blog.janjonas.net/2012-09-05/asp_net-mvc_4_rc-web-api-return-http-status-code-4xx-invalidoperationexception-no-mediatypeformatter-available-read-object-type-content-media-type
You can certainly use your own custom DelegatingHandler or you can make your custom HttpModule
which will implement the IHttpModule and check the current http request and validate the request.

asp.net not deserializing soap response

I have been given a wsdl and have used wsdl.exe to create my proxy classes.
I am able to call the function to initiate the request with some valid parameters and this returns my response object which is always EMPTY.
When i inspect the soap message response using fiddler the soap does have valid data that should be deserialzed to the proxy classes.
Can i manually intercept the derserializing call of the proxy classes generated by wsdl and check that .net is correctly derializing the soap response?
Thank you
The empty object is most likely the result of a mismatch between the soap message and your proxy class. This can for example be caused by a difference in namespaces (newer version).

Resources