Is possible to get HTTP status code in side SOAP Extensions C#? - c#

I create C# webservice with soap extension base on this article :
https://msdn.microsoft.com/en-us/library/esw638yk(v=vs.71).aspx
for keep soap log,Is it possible to get http status code in soap extension section.

I got an answer by add
HttpContext ctx = HttpContext.Current;
String HTTPCODE = ctx.Response.StatusCode.ToString();

Related

WCF: request/response with different content types

I'm trying to consume a SOAP service using WCF in .NET 5. The service provider expects the content type of the request to be text/xml but sends a response with content type application/xml.
The following code throws a ProtocolException because is expects both request and response to have content type text/xml.
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://[...]/inquiry");
var channelFactory = new ChannelFactory<InquiryServiceSoapPort>(binding, endpoint);
var serviceClient = channelFactory.CreateChannel();
var inquiry = new Inquiry();
var result = serviceClient.createInquiry(inquiry);
I was able to change the content type to application/xml using a custom encoder, but that changes it for both request and response, and my request then is rejected by the server.
Is there a way to change the content type of the response only?
Edit
I am writing client-side code, not server-side code, mind you. As of now, it seems I can only catch and swallow the exception or re-implement the code either using another framework or from scratch.
Have a look at the WCF architecture here:
https://learn.microsoft.com/en-us/dotnet/framework/wcf/architecture
So we have the ABC of WCF is the Address, Binding and Contract, and here we are in the contract serialization
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-transfer-and-serialization
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-serializer
in detail:
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-5.0
the are the concept i recommend diving into, the http header type "content-type" is really a function of the rest in case You make the BasicHttp binding
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding?view=dotnet-plat-ext-5.0
Which is sort of only the corner of a much bigger tool available in WCF. for instance wsHttpBinding which is really preferable in many scenarios, could alone solve your problem if the endpoint is available.
In the wsHttpBinding You can control the message encoding
https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/wshttpbinding
Ultimately choosing your Message encoder will determine how headers are set, there is a wealth of abstraction level(s) compared to REST io model, but it's there:
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/choosing-a-message-encoder
And You can even roll You own.

Error in response from PUT method using compression

I am using gzip compression in my .net core api. Responses from HTTP GET methods are all ok. When this method is a HTTP Put (example) the response body is corrupted. My put method response is the same data from request with additional information.
Have a additional configuration for this case?
This is my configuration:
services.AddResponseCompression(opt =>
{
opt.Providers.Add<GzipCompressionProvider>();
opt.EnableForHttps = true;
});
services.Configure<GzipCompressionProviderOptions>(opt => opt.Level = CompressionLevel.Fastest);
Thanks for all interested.
I have make a extensive debugging and found the problem: some classes with a circular reference caused serialization error.

Set Request Header SharePoint web service

I am using the 2010 SharePoint Lists web service to return a Content Type and it's fields through a c# application. The Library is in a web whose language is set to welsh, but with an alternate language of english set. This means if the internet options in the browser are set to english, the library displays in English.
I have been able to set the Accepts-Language header for requests made using the client object but have not been able to do so for the web service.
Is it possible to see a header on requests made through the SharePoint Web Services, and if so, how?
In case of ASMX web services you could consider the following approach. SoapHttpClientProtocol Class contains GetWebRequest Method that could be used for specifying a custom headers.
Once the proxy class is generated, create a class that derives from it and set custom header as shown below:
public class ListsEx : Lists
{
protected override WebRequest GetWebRequest(Uri uri)
{
var request = base.GetWebRequest(uri);
//Add the Accept-Language header (for Danish) in the request.
request.Headers.Add("Accept-Language:da");
return request;
}
}
where Lists is the name of generated proxy class.
Usage
using (var client = new ListsEx())
{
client.Url = webUri + "/_vti_bin/Lists.asmx";
var reult = client.GetList("Pages");
//...
}
Result
I don't know what type of web services you use. In case of WCF services, you can use: How to add a custom HTTP header to every WCF call?
In case of ASMX web services: Adding SOAP headers to ASMX service requests .
In both cases use Accept-Language header like "Accept-Language:en"

How to call a web request before deserialize JSON

Attempting to get a web request before I attempt to deserilize my url unfortunately none of the conventional methods for making web requests work.
public WeatherModel GetWeather(string url)
{
WeatherModel WeatherData = new JavaScriptSerializer().Deserialize<WeatherModel>(url);
return WeatherData;
}
Does anyone have any suggestions?
You need to send a HTTP request to the URL and parse the response data. You cannot simply deserialize the URL.
Use HttpClient class to send HTTP requests.
https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx
Please refer to this link to see how to make HTTP requests using HttpClient.

Getting latlong from address using google geocoding

I am using google geocoding api to get latitude and longitude from address.But it gives an exception
The remote server returned an error: (403) Forbidden.
The code is as below:
WebClient client = new WebClient();
Uri uri = GetGeocodeUri(address);
string[] geocodeInfo = client.DownloadString(uri).Split(',');
return new Coordinate(Convert.ToDecimal(geocodeInfo[2]),convert.ToDecimal(geocodeInfo[3]));
The exception comes from line 3 at the time of "downloadstring".
Please help me out.
Was this working before and now stopped working? GeoCoding has usage limits and can return a 403 if your going over those limits.
https://developers.google.com/maps/documentation/business/articles/usage_limits
snip it:
HTTP 403 response
Requests to the web services may also receive a HTTP 403 (Forbidden)
error. In most cases, this is due to an invalid URL signature. To
verify this, remove the client and signature parameters and try again:
If the response is HTTP 200 (OK), the signature was the problem. This
is not related to usage limits; see Generating Valid Signatures in the
Web Services chapter of the Maps API for Business documentation for
details. If the response is still a HTTP 403 (Forbidden) error, the
signature was not necessarily the problem, it may be related to usage
limits instead. This typically means your access to the web service
has been blocked on the grounds that your application has been
exceeding usage limits for too long or otherwise abused the web
service. Please contact Google Enterprise Support if you encounter
this issue. Requests to all web services require URL signatures.
Requests will also be rejected with a HTTP 403 (Forbidden) error when
including the client parameter but missing the signature parameter, or
vice versa.
var loc = await GetLocation("central park");
public static async Task<GeoCoordinate> GetLocation(string address)
{
string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
string json = await wc.DownloadStringTaskAsync(url + address);
dynamic result = await JsonConvert.DeserializeObjectAsync(json);
var loc = result.results[0].geometry.location;
return new GeoCoordinate((double)loc.lat, (double)loc.lng);
}
}

Categories

Resources