setting a proxy on HttpClient that comes with WCF Web Api? - httpclient

Does anyone know of a way to set a proxy on httpclient that comes with the wcf web api? I just want to be able to monitor all the traffic with fiddler.

You don't need to set a proxy on HttpClient to use fiddler. If you are hosting on localhost then check these tips here http://www.fiddler2.com/fiddler/help/hookup.asp#Q-LocalTraffic.
if you really want to use a proxy then you can do it like this,
var clientHandler = new HttpClientHandler();
clientHandler.Proxy = new WebProxy(new Uri("http://..."));
var httpClient = new HttpClient(clientHandler);

Related

How to implement STTP SSL requests with HttpClientZioBackend? (SCALA/ZIO)

I am using SttpAPI to send http requests, for one of my requests I need to use mutual SSL and i'm failing to find instructions how to implement it.
My backend is HttpClientZioBackend and there is no documentation for customizing the SSL Context for this kind of backend.
I will appreciate any help or samples.
val httpClient: HttpClient = HttpClient.newBuilder().sslContext(ssl).build()
HttpClientZioBackend.usingClient(client = httpClient, ...)

Http post requests unsing NTLM Authentication (java)

I tried to send a HttpRest Call using NTLM Autentication in Java. I tried using the org.apache.http library. it was not a big problem to use the HttpClient to send a Post Request with anonymos authentication.
But i have some troubles with WindowsAuthentication. I even tried to use CredentialProvider with my own Windows credentials (as a compromise, i dont like that either) but i didn’t succeed.
Is there an easy way using NTLM Authentication sending post requests from Java code?
Is there another lib which fits better form my needs?
I still have no idea why the doku from https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html about NTLM Authentication didn’t have worked for me.
I finally solved my problem doing it similar to the documentation for basic authentication as described on http://www.baeldung.com/httpclient-post-http-request
it now looks like this:
...
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new NTCredentials("username", "passwd", hostname, "domain.at"));
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
HttpPost post = new HttpPost("http://www.example.com"));
StringEntity input = new StringEntity(bodyAsString, HTTP.UTF_8);
input.setContentType("application/json");
input.setContentEncoding("UTF-8");
post.setEntity(input);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(post);
...

Viewing SOAP returned by a web service

I created a web service reference in my VS2010 project, and configured it with a WSDL service address URL. VS2010 created a nice proxy class for me to consume the web service.
I am getting a result that I don't like, and, in an effort to troubleshoot, I'd like to see the XML coming back from the web service. What is the simplest way to do so? I'd like to be able to do it within my Visual Studio debugging session, but if I have to go outside that, so be it.
I am trying to make the following work:
Dim response As HttpWebResponse = Nothing
Dim reader As System.IO.StreamReader = Nothing
Dim hwrResponse As HttpWebResponse = DirectCast(**request**.GetResponse(), HttpWebResponse)
Dim responseStream As System.IO.Stream = hwrResponse.GetResponseStream()
Dim xtrSmp As New System.Xml.XmlTextReader(responseStream)
Dim strXm As String = xtrSmp.ReadInnerXml()
xtrSmp.Close()
hwrResponse.Close()
but I don't know what my request should be.
Your REQUEST should be a REQUEST Object whiich is configured to access the SOAP service you are trying to access...
You can find out more here...
HTTPWEBRESPONSE object http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx
HTTPWEBREQUEST object http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
Use one of theseWebServiceStudio (http://webservicestudio.codeplex.com/),
Storm (http://storm.codeplex.com/)
or the outstanding Eclipse WSDL Editor and Web Service Explorer (look at http://wiki.eclipse.org/index.php/Introduction_to_the_WSDL_Editor)
These will let you invoke your service methods with an ad-hoc GUI client generated from WSDL and access both request and response XML.
The list of tools above is of course incomplete.

Getting HttpStatus from WCF Rest Client

As i've mentioned in topic. I have WCF Rest Service, and ASP.NET MVC3 client. In ASP's controller i'm using
IMyService serviceClient = new WebChannelFactory<IMyService>().CreateChannel();
to create client (channel) for my service. Is there any way to attach to this channel and get HttpStatusCode for each response from WebService? Btw should I close channel after each request-response? or it could be opened for next EndUser requests via ASP.NET MVC3 app?
I'm setting HttpStatusCode in webService through
WebOperationContext.Current.OutgoingResponse.StatusCode = <HttpStatusCode>;
And i'd like to check it in MVC3 app and show proper hint.
EDIT:
Nevermind. I've found answers.
http://msdn.microsoft.com/en-us/magazine/cc163302.aspx
AnswerLink

Defaulthttpclient scheme

The request is using the wrong scheme (http instead of https). I can see this when i debug my client and inspect the scheme inside the HttpHost object. I'm using JerseyClient to submit the request, it does so by creating a web resource with a URI. I simply pass a string https://myserver.com:443/some/path. However inside the DefaultHttpRoutePlanner class it decides to use the default settings for HttpRoute and uses http. Can anyone tell me how i can override the default settings of the HttpRoute or RoutePlanner classes?
found the answer -
return new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme());