unable to view http traffic when calling DefaultHttpClient in a Spring controller - http

I have a Spring MVC controller that makes a json RESTful webservice call using apache DefaultHttpClient. I wanted to view the http request/response data of that webservice call, I've tried firebug, wireshark, fiddler, but had no success.
they do show the traffic when I'm using a browser.
below is the jist of the webservice call
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "8888"); // set proxy to fiddler
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(targetUrl);
// sample targetUrl = "http://localhost:9080/SampleBackend/sample-backend-json.jsp"
StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}");
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response2 = httpClient.execute(postRequest);

You must run the packet capture (tcpdump most likely) on the loopback interface of the server. You can then download the captured data and view it in wireshark, same as a live capture.

Related

Can I post json object from model class to angular .ts file

I am retrieving XML result from thrid-party API to ASP.net web API. Now I want to send this retrieved XML( converted in JSON) to angular typescript file. How can I achieve it?
My approach 1. send JSON from model class to controller and then from the controller to angular service or component's .ts file.
2.post from model class to component's .ts file.
var objJson = new JavaScriptSerializer().Serialize(responseB);
var response = await client.PostAsync("http://localhost:4200/add", new StringContent(objJson, Encoding.UTF8, "application/json"));
var responseString = await response.Content.ReadAsStringAsync();
You can't send something "from a WebAPI to a TypeScript file".
It sounds like you want to send it from the server to a client app in a web browser? If this is the case, then you need the client app to ask for it, or to push it to the client.
If the client initiated the request that had you fetch this resource, then you could send it in the response to the client.
If the client app has some bi-directional connection to your server like socket.io or SignalR, you can send it over that.
Or, depending on the frequency, probability of this happening, you might have the client poll for it.
But it all depends on the state machine of the entire process you are implementing. Does the client app initiate any of this, or this an event that the client does not initiate, but needs to be informed of? If this is the case, you either need to poll or to set up a connection from the client to the server.

Xamarin Form not able to invoke the Http URL

I am new to Xamarin Cross-Flatform technology (C#). I am developing one small application where I need to call the http url, get the json data, parse it and display it on the screen.
I am using System.Net.Http for achieving the http call.But request is not reaching to http url
Regards,
Amit Joshi
You can use RestSharp for making http calls.
It is very easy to use.
Code sample:
using RestSharp;
var client = new RestClient ("http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/");
var request = new RestRequest (String.Format ("{0}/allinfo", "198440"));
client.ExecuteAsync (request, response => {
Console.WriteLine (response.Content);
});
RestSharp Examples

Can't see WebClient post request in Fiddler

I have an ASP.NET WebForms app (sender) which sends a WebClient post request to another ASP.NET app (receiver) on the same dev machine. The WebClient post is initiated by clicking a button in the sender app. It's a test app and the form has only the button. I can see the post from the button in Fiddler but I don't see the post request from the WebClient method. Why?
I know the WebClient post runs successfully because the breakpoint is hit in the receiver app and the Forms collection has the value of the input field from the WebClient request from the sender app. (Using Windows 8.1)
Update This is the call:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var data = "FirstName=John";
var result = client.UploadString("http://localhost/testform/default.aspx", "POST", data);
Console.WriteLine(result);
}
.NET and IE(before version 9) are not sending requests to localhost through any proxies. There are 3 possible solutions:
Use machine name or hostname: http://<machine name>/testform/default.aspx
Add ipv4.fiddler to the URL: http://localhost.fiddler/testform/default.aspx
Add custom rule to the fiddler:
static function OnBeforeRequest(oSession:Fiddler.Session){
if (oSession.HostnameIs("MYAPP")) {
oSession.host = "<put your ip address and port here>";
}
}
Then you should be able to capture traffic through http://myapp/testform/default.aspx
Reference Problem: Traffic sent to http://localhost or http://127.0.0.1 is not captured.
Could be multiple things. Here are some possibilities
You have Fiddler set to filter to only show things from a particular process (or some other type of filter but process is the easiest one to accidentally turn on)
You have not turned on HTTPS capture in Fiddler but this missing request is HTTPS (it's off by default)
Your WebClient has a custom proxy configured and isn't pulling the default settings from IE

SignalR wth gzip compression

Having some problem developing a SignalR client for a Hub hosted in asp.net website with gzip compression enabled. Since we are using IIS compression, the response from SignalR also gets compressed, but, the client does not understand the response and we get a Json parsing error on the client side.
SignalR internally uses HttpWebRequest to make make http requests and HttpWebRequest can be configured to automatically decompress the response using AutomaticDecompression property. So, if somehow I can get hold of the HttpWebRequest object used by SignalR to make the request, I should be able to set the enable automatic decompression.
I thought I should be able to get access to the HttpWebRequest by providing HubConnection.Start with my custom implementation of IHttpClient, IHttpClient.GetAsync takes a prepareRequest action which I thought should give me access to the HttpWebRequest, but, HttpHelper.GetAsync wraps the HttpWebRequest with HttpWebRequestWrapper before passing to prepareRequest and HttpWebRequestWrapper does not provide access to HttpWebRequest.
HttpHelper class is internal so can't use it as well, so, I am not exactly sure how to enable automatic decompression with SignalR.
I can expose the HttpWebRequest in HttpWebRequestWrapper, but, would prefer a simpler solution if one exists. Any thougths?
I am using SignalR version 0.5.1.10822
My auto decompression HttpClient:
public class HttpClientWithAutoDecompression : IHttpClient
{
readonly DefaultHttpClient _httpClient = new DefaultHttpClient();
private readonly DecompressionMethods _decompressionMethods;
public HttpClientWithAutoDecompression(DecompressionMethods decompressionMethods)
{
_decompressionMethods = decompressionMethods;
}
public Task<IResponse> GetAsync(string url, Action<IRequest> prepareRequest)
{
Task<IResponse> task = _httpClient.GetAsync(url,
request =>
{
[ERROR: request is actually HttpRequestWrapper and
does not expose HttpWebRequest]** ]
var httpWebRequest = (HttpWebRequest) request;
httpWebRequest.AutomaticDecompression = _decompressionMethods;
prepareRequest(request);
});
return task.ContinueWith(response =>
{
Log.Debug(this, "Response: {0}", response.Result.ReadAsString());
return response.Result;
});
}
....
}
To the best of my knowledge GZip encoding and streaming do not mix. In the case of the forever frame transport the client wouldn't be able to decode any on the streaming content until the entire response, or at least a significant block of data, is received (due to the way the data is decoded). In the case of web sockets there is not support for encoding of any kind at this time, although there is apparently an extension to the specification for per message encoding being worked on.
That said, if you wanted to attempt to provide support for the LongPolling transport, the only way I can see this being possible is to provide your own SignalR IHttpClient implementation. You can see right now that the DefaultHttpClient class uses HttpHelper::GetAsync which creates the HttpWebRequest internally and you can never get your hands on that because you only have access to the IRequest which is HttpWebRequestWrapper at that point.
By creating your own IHttpClient you can take over the initial instantiation of the HttpWebRequest, set the AutomaticDecompression and then wrap that up yourself with the HttpWebRequestWrapper.

how can i invoke facebook http rest api whitout facebook api lib's (java or C++)

i like to preform simple facebook api call via http rest
but whiteout using facebook java/c++ pre made lib
plain http call
i already done the authorization part and i have the session id and all that .
i just like to see what i need to preform api call over http
thanks
Just use the Facebook Rest API to create your URLs and then send the response via POST
from http://www.exampledepot.com/egs/java.net/Post.html
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

Resources