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"
Related
I have a program that submits data to a third party web service. They are implementing two-factor authentication where I have to create a nonce key (using NewGuid), they provide an encoded API key to include, as well as a screen name, URL and time stamp.
I have the nonce being created, and then using the api key, I sign the data and in the end, I'll have a long string such as this:
2017-04-15T17:08:57Z-1265fb1e-bbc3-453a-be409e2a808cbaaeWpyp6EJnIUlSX1rB/YRJxRyp8cXxw2IIrFMnnvuB06cUBabyRLnD 5hPj+ndH8zSIhojvNgc10/az2N+hh6SaMA==
It states that it would be sent in an HTTP header called X-WME-API-Token.
We have a MeterDataService.cs file that was generated from the SVCUTIL command line tool that creates a proxy client. I don't see anywhere in there that I can add the customer header.
I know that you can do something like this if you have something derived from WebRequest:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlString);
req.ContentType = "text/xml";
req.Headers["X-SPP-API-Token"] = Token;
req.Method = "POST";
However, none of the resulting objects is derived from WebRequest, so I'm not sure how to do this.
One object, the MeterDataPortClient object, has an Endpoint property. Is that where I would do it? How do I do that? I guess that's my question.
I did try modifying the MeterDataRequest object they created by having it inherit from WebRequest, but then I got this error:
"The type PostMeterDataRequest defines a MessageContract but also derives from a type System.Net.WebRequest that does not define a MessageContract. All of the objects in the inheritance hierarchy of PostMeterDataRequest must defines a MessageContract."
Here's the the definition of the PostMeterDataRequest and I see the MessageContractAttribute being applied to the class. I'm just not familiar with what that means. I just need to find a way to add the HTTP header to the request object before it's sent to the web service since the service at the receiving end expects the token to be in the HTTP header and I have no idea how to put it in with the code that I have at hand.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class PostMeterDataRequest : WebRequest // WebRequest added my me later on.
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://someorg.com/schema/MeterDataSchema/v2", Order = 0)]
public PostMeterDataType PostMeterData;
public PostMeterDataRequest()
{
}
public PostMeterDataRequest(PostMeterDataType PostMeterData)
{
this.PostMeterData = PostMeterData;
}
}
Finally found a solution. It was this earlier post that I never found before as much as I searched, but it worked.
How to add Custom HTTP Header for C# Web Service Client
Following the directions in the first answer worked beautifully.
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();
I have registered a new content type in ServiceStack with:
appHost.ContentTypeFilters.Register("application/x-my-content-type",
SerializeToStream, DeserializeFromStream);
And everything works as expected, if the client sends the content type in the http stream.
Unfortunately, I have a client that is not in my control of HTTP Request Heads and does not send the content type.
How can I get ServiceStack to set the default content type for that route?
On every ServiceStack /metadata page lists the different ways a client can request a specific Content-Type:
To override the Content-type in your clients HTTP Accept Header, append ?format=xml or add .format extension
E.g. The client can specify your custom ContentType with ?format=x-my-content-type, adding .x-my-content-type extension or by specifying the HTTP Header (in the HttpClient):
Accept: application/x-my-content-type
Otherwise if your HttpClient doesn't send an Accept header you can specify the default content type in your AppHost with:
SetConfig(new HostConfig {
DefaultContentType = "application/x-my-content-type"
});
Note: All Configuration options in ServiceStack are set on HostConfig.
The issue when calling web services from a web browser is that they typically ask for Accept: text/html which by contract ServiceStack obliges by returning back HTML if it is enabled.
To ensure your Content-Type is always returned you may also want to disable the HTML feature with:
SetConfig(new HostConfig {
EnableFeatures = Feature.All.Remove(Feature.Html),
});
Otherwise if you want to override the Accept header you can force your service to always return your Content-Type by decorating your Response DTO inside a HttpResult, i.e:
return new HttpResult(dto, "application/x-my-content-type");
Otherwise anywhere outside of your Service (e.g. Request/Response filter) you can set the Response ContentType anywhere that has access to a IHttpRequest with:
httpReq.ResponseContentType = "application/x-my-content-type";
I have a simple REST web service in WCF that is declared as follows:
[WebInvoke(Method = "PUT",UriTemplate = "comptatge/add",RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
public void GravaComptatge(MyDataContract contract)
{
...
}
where MyDataContract is a class with simple properties.
Now I send a put Request using fiddler to see if it works OK. In the body I send JSON with the same properties as MyDataContract but it's not working. I get a 400 Bad Request HTTP error. What am I doing wrong here? The service is hosted in a MVC app using ServiceRoute. Other GET services work OK.
This happened to me before, and adding Content-Type: application/json in the request headers was the solution.
Is there a C# or .NET class to handle HTTP Content Negotiation with a User Agent?
I would like to be able to supply a list of acceptable Content Types, and have those negotiated with the browser to find the best match.
I recently wrote a content negotiation library in F#.
I blogged about it here.
I think the word user agent is a bit off in your question but if you want to build request a certain source (lets say a restfull api). You can use the WCF Rest Starter kit (http://wcf.codeplex.com/) to specify the type of content you want or accept:
HttpClient client = new HttpClient(new Uri("http://restfull/api/"));
//this is XML but could be JSON or whatever the API can supply
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var response = client.Get(path);
if (response != null)
{
response.EnsureSuccessStatusCode();
//this will be XML
string xml = response.Content.ReadAsString();
}