Reading response from proxy using WinHttpQueryHeader() - winhttp

How to find out if the response is sent from the proxy or end-server by using WinHttpQueryHeader() function? I have tried using WINHTTP_QUERY_PROXY_AUTHENTICATE as the dwInfoLevel paramter to the function winHttpQueryHeader() but the buffer I got is NULL.
int bufferSize = 0;
char lpOutBuffer[500];
ReturnVal = WinHttpQueryHeader(Handle, WINHTTP_QUERY_PROXY_AUTHENTICATE, NULL, &bufferSize);
ReturnVal= WinHttpQueryHeader(Handle, WINHTTP_QUERY_PROXY_AUTHENTICATE, lpOutBuffer, &bufferSize);
lpOutBuffer is NULL.
Actually what I think the problem here is that I get the bufferSize 0 in the first WinHttpQueryHeader and so the second WinHttpQueryHeader does not fill the lpOutBuffer (since bufferSize is 0). Why is the bufferSize returned as 0?
Contrary to this, if I use the InfoLevel as WINHTTP_QUERY_RAW_HEADERS_CRLF, I do get the correct output and bufferSize is not 0, it gets filled after the first WinHttpQueryHeader() call..

If a HTTP server requires authentication the HTTP response code is 401 and the corresponding response header is WWW-Authenticate. If a proxy server requires authentication the HTTP response code is 407 and the corresponding response header is Proxy-Authenticate. I suggest checking the response for the proper HTTP code and then retrieving the corresponding header.
As for the WINHTTP_QUERY_PROXY_AUTHENTICATE query flag, the documentation states:
Retrieves the header that is used to identify the user to a proxy that
requires authentication. This header can only be retrieved before the
request is sent to the server.
So that particular flag has nothing to do with a response from the server. It is a request header that is sent to identify the user to the proxy.

Related

How to extract sessionId from Citrus HttpRequest

I tried to test the a set of REST services using Citrus Java DSL. After authentication the services expect the same, valid session id of the first request.
On the server side I can see, that there exists a random session-id, but at the second request, the session-id is null.
I've tried to set handleCookies to true in the endpoint configuration and tried to extract some header information (set-cookie) but without success. The EndpointConfiguration is reused during the different requests.
CitrusEndpoints.http()
.client()
.handleCookies(true)
How can I force the Endpoint to reuse the negotiated session-id or how can I extract it from the request / response?
Thanks in advance for any ideas and hints.
The response to your 1st request should have a header set
Set-Cookie: JSESSIONID=ABCDEFG;path=/api/foo
You can extract this information in your receive operation
http()
.client(todoClient)
.receive()
.response(HttpStatus.OK)
.extractFromHeader("Set-Cookie", "cookie")
.payload("{ \"foo\": \"bar\" }");
After that we have to post process the new ${cookie} value in order to extract the actual session id name and value into a new variable ${sessionId}.
createVariable("sessionId", "citrus:substringBefore(${cookie}, ';')");
Now we have a variable ${sessionId} that only contains the name and value of the session id - in our example this is JSESSIONID=ABCDEFG.
In further requests you can use the variable in order to set proper Cookie header information
http()
.client(todoClient)
.send()
.get("/api/foo")
.header("Cookie", "${sessionId}")
.accept(ContentType.APPLICATION_JSON.getMimeType());

Use Response of a wcf request as request in another receive port

Is there any way to use the response of a wcf service method request as an input for the next request in same orchestration and return the response of the first request as well as the response of the second request as out put in BizTalk?
Eg :
My first request gives a response as "a"
Give this response "a" as request to the 2nd request and gets the response as "b"
Return the response as "a" and "b".
Is this possible?
Yes. You could either create a map from Response 1 to Request 2, and also create a multiple input message map from Response 1 and Response 2 to your final output message.
If the messages involved don't have any repeating structures, it may be enough to distinguish the fields that you need to be concerned with and just use a ConstructMessage with an XmlDocument, i.e.
// construct shape code
varXmlDoc = new System.Xml.XmlDocument();
varXmlDoc.LoadXml("<webSvcRequest2 xmlns=''><ParamB>" + msgWebSvcResp1.ParamA + "</ParamB></webSvcRequest2>");
msgWebSvcReq2 = varXmlDoc;
And similar code for producing the final output message. If you go this route, I'd advice creating some C# utility methods to actually store the strings/message templates.

HTTP Content Type returned by server when a response is not required

When returning html to the client, I set the server content type to:
"text/html"
When I returning JSON to the client, I set the server content type to:
"application/json"
I have a special case where the client makes an AJAX call to do something unimportant and the client really doesn’t benefit by getting a reply from the server so I just return an empty reply and set the server content type to:
"application/json"
Is this OK even though my reply is empty?
Thank you

ASP.NET Web API - Reading querystring/formdata before each request

For reasons outlined here I need to review a set values from they querystring or formdata before each request (so I can perform some authentication). The keys are the same each time and should be present in each request, however they will be located in the querystring for GET requests, and in the formdata for POST and others
As this is for authentication purposes, this needs to run before the request; At the moment I am using a MessageHandler.
I can work out whether I should be reading the querystring or formdata based on the method, and when it's a GET I can read the querystring OK using Request.GetQueryNameValuePairs(); however the problem is reading the formdata when it's a POST.
I can get the formdata using Request.Content.ReadAsFormDataAsync(), however formdata can only be read once, and when I read it here it is no longer available for the request (i.e. my controller actions get null models)
What is the most appropriate way to consistently and non-intrusively read querystring and/or formdata from a request before it gets to the request logic?
Regarding your question of which place would be better, in this case i believe the AuthorizationFilters to be better than a message handler, but either way i see that the problem is related to reading the body multiple times.
After doing "Request.Content.ReadAsFormDataAsync()" in your message handler, Can you try doing the following?
Stream requestBufferedStream = Request.Content.ReadAsStreamAsync().Result;
requestBufferedStream.Position = 0; //resetting to 0 as ReadAsFormDataAsync might have read the entire stream and position would be at the end of the stream causing no bytes to be read during parameter binding and you are seeing null values.
note: The ability of a request's content to be read single time only or multiple times depends on the host's buffer policy. By default, the host's buffer policy is set as always Buffered. In this case, you will be able to reset the position back to 0. However, if you explicitly make the policy to be Streamed, then you cannot reset back to 0.
What about using ActionFilterAtrributes?
this code worked well for me
public HttpResponseMessage AddEditCheck(Check check)
{
var request= ((System.Web.HttpContextWrapper)Request.Properties.ToList<KeyValuePair<string, object>>().First().Value).Request;
var i = request.Form["txtCheckDate"];
return Request.CreateResponse(HttpStatusCode.Ok);
}

Upload file to Solr with HttpClient and MultipartEntity

httpclient, httpmime 4.1.3
I am trying to upload a file through http to a remote server with no success.
Here's my code:
HttpPost method;
method = new HttpPost(solrUrl + "/extract");
method.getParams().setParameter("literal.id", fileId);
method.getParams().setBooleanParameter("commit", true);
MultipartEntity me = new MultipartEntity();
me.addPart("myfile", new InputStreamBody(doubleInput, contentType, fileId));
method.setEntity(me);
//method.setHeader("Content-Type", "multipart/form-data");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse hr = httpClient.execute(method);
The server is Solr.
This is to replace a working bash script that calls curl like this,
curl http://localhost:8080/solr/update/extract?literal.id=bububu&commit=true -F myfile=#bububu.doc
If I try to set "Content-Type" "multipart/form-data", the receiving part says that there's no boundary (which is true):
HTTP Status 500 - the request was rejected because no multipart boundary was found
If I omit this header setting, the server issues an error description that, as far as I discovered, indicates that the content type was not multipart [2]:
HTTP Status 400. The request sent by the client was syntactically incorrect ([doc=null] missing required field: id).
This is related to [1] but I couldn't determine the answer from it. I was wondering,
I am in the same situation but didn't understand what to do. I was hoping that the MultipartEntity would tell the HttpPost object that it is multipart, form data and have some boundary, and I wouldnt set content type by myself. I didn't quite get how to provide boundaries to the entities - the MultipartEntity doesn't have a method like setBoundary. Or, how to get that randomly generated boundary to specify it in addHeader by myself - no getBoundary methor either...
[1] Problem with setting header "Content-Type" in uploading file with HttpClient4
[2] http://lucene.472066.n3.nabble.com/Updating-the-index-with-a-csv-file-td490013.html
I am suspicious of
method.getParams().setParameter("literal.id", fileId);
method.getParams().setBooleanParameter("commit", true);
In the first line, is fileId a string or file pointer (or something else)? I hope it is a string. As for the second line, you can rather set a normal parameter.
I am trying to tackle the HTTP Status 400. I dont know much Java (or is that .Net?)
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error