How do I know if the data I pushed or pinged is received? in .net - push

I do not know if my question is correct but can you guys help me with my code. I pinged(dont know if that's the correct term for that) a website let's say samplewebite.com (just a sample) using the code below. Now what will I insert or what code do I have to insert or what do I have to do to check if that webite received the data i pushed or did I pushed the data with this code? please help.
Thanks in advance.
enter code here
private void btnLogin_Click(object sender, EventArgs e)
{
string username = this.txtUsername.Text;
string password = this.txtPassword.Text;
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(username, password);
client.DownloadString("http://samplewebite.us/");
}
this.Dispose();
}

Try to clarify your question: I'm sorry but I can't understand what you are asking for...
Your code is issuing an HTTP GET request to the site, if the response from the server is an HTTP 200 OK (or another success code) then the DownloadString call returns the body of the response.
Otherwise (HTTP 4XX / 5XX or timeouts) it throws a WebException.
http://msdn.microsoft.com/en-us/library/fhd1f0sw.aspx

Related

SoapUI 5.7.0 mockRequest.requestContent is empty for POST request

I am using SOAP UI 5.7.0 to mock a REST service and it is working fine. Only, when I want to access the body of a POST request with mockRequest.requestContent, the correct content is returned only in the first call, but from then on, it always returns the empty string.
I tried the call in OnRequestScript and in the POST requests own Dispatch script but the behavior is the same in both cases. I have the suspicion, that the Stream of the request is already read somewhere else and so does not return any more content. But I don't know what to do about it.
I wonder what is the correct way to read the content of a POST request.
Thank you
Appears to be a known issue, see posts in the community forum here and here.
this seems to be an old bug of PUT operation in REST mocks.
Unfortunately, this is a bug. There is an internal defect of SOAP-2344 for this issue. This applies for the PUT and DELETE methods for a REST mock service.
I have the same issue with a PATCH request.
Use the following trick to get the body of the PUT and DELETE requests:
mockRequest.with {
if (method.toString() == 'PUT' || method.toString() == 'DELETE') {
InputStreamReader isr = new InputStreamReader(request.getInputStream(), "UTF-8")
BufferedReader br = new BufferedReader(isr)
StringBuilder sb = new StringBuilder()
while ((s=br.readLine())!=null) {
sb.append(s)
}
def requestBody = new groovy.json.JsonSlurper().parseText(sb.toString())
log.info "requestBody: " + requestBody
}
}
I use it on the project but I don't really remember how where I got the snippet from. I had to change some parts to make it work as far as I remember. Give it a try.

Can I retrieve a file from server with GET in rest

Can I return a file with my get request? I want to return a word document to calling angularJS service through REST GET method. Not sure if it is even possible.
You're a bit light on detail, so I'm gonna be a bit light on answer.
A REST request is just... a request. The REST side of things is more the way URLs are defined that what actually happens in the request process itself, which is all still vanilla HTTP.
So, same as with any GET request, if you want to return binary data, set the headers appropriately (<cfheader>), then return the file (<cfcontent>).
So this is how I did it, luckily I got this:
http://smithcustomdev.com/2010/10/20/download-file-to-browser-using-cfscript/
All I have to do was make the method remote and listen to REST service
<cfscript>
private void function serveFile(string filePath){
var fileContent = fileRead(expandPath(filePath));
var context = getPageContext();
context.setFlushOutput(false);
var response = context.getResponse().getResponse();
response.reset();
response.setContentType("text/csv");
response.setContentLength(len(fileContent));
response.setHeader("Content-Disposition","attachment; filename=#listLast(filePath,'\')#");
var out = response.getOutputStream();
out.write(ToBinary(ToBase64(fileContent)));
out.flush();
out.close();
}
</cfscript>

GWT: What to expect when an HTTP request fails

I'm sending an HTTP request with RequestBuilder.send(). I would expect to see successful responses come back in onResponseReceived(), and errors come back in onError(). But that's not what I'm seeing. GWT calls onResponseReceived() regardless of success or failure.
Does anyone know what I should really expect? Do you have any information that would help me detect and report errors better?
Here's some sample code:
builder.sendRequest(null, new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
Header[] debugHeaders = response.getHeaders();
String debugHeaders1 = response.getHeadersAsString();
int debugStatusCode = response.getStatusCode();
String debugStatusText = response.getStatusText();
String debugText = response.getText();
handleResponse(response);
}
#Override
public void onError(Request request, Throwable exception) {
reportError();
}
});
I can force an error on my computer by disabling the Wi-Fi. In that case onResponse() is called. getHeaders() returns an array of length 1, with the only entry set to null. getHeadersAsString returns "". getStatusCode returns 0. getStatusText() returns "". getText() returns "".
If this is always the case, I can look for that in my code. However, I expect that different browsers, different errors, etc, will cause a different result. Is there any good way to always detect an error?
(As long as there are no HTTP problems, my code works fine.)
This the expected behavior; see comments in: https://code.google.com/p/google-web-toolkit/issues/detail?id=2858
According to documentation onResponseReceived is called in both cases (success or not).
I got the same on old browsers when my browser tried to go to download something and the prev. http request was not completed. So maybe try to wait untill the response is completed, maybe try to add some 200 msec. delay somewhere.
In my application i ignore when status code is 0.

GWT - Way around Access-Control-Allow-Origin?

I'm making an internet application with GWT, and one of the features that I've been stuck on for a few weeks is getting the users contact data from google data. I've tried things like GWT-GData and they don't seem to play nicely with the current version of GWT, so I tried going the more traditional approach with OAuth and doing an HTTP Get request. I haven't been receiving anything back as a response, and couldn't figure out why, and I happened across my javascript error log and I'm getting:
"Origin [site name here] is not allowed by Access-Control-Allow-Origin"
I've done some reading and I have a decent idea of what's going on, but I don't know how to get around it in GWT. I've found plenty of read-ups on how to get around it with other platforms, but I haven't seen anything for GWT. Can anyone offer any wisdom?
Edit:
Here is the code I'm using:
public static void doGet(String url, String oauthToken) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(oauthToken, new RequestCallback() {
#Override
public void onError(Request request, Throwable e) {
GWT.log(e.toString(), e);
}
#Override
public void onResponseReceived(Request request, Response response) {
Window.alert("HEADER:" + response.getHeadersAsString()
+ "\nSTATUS: " + response.getStatusText()
+ "\nTEXT: " + response.getText());
}
});
} catch (RequestException e) {
GWT.log(e.toString(), e);
}
}
There's nothing you can do but configure the server to accept the origin of the request (i.e. add it to the returned Access-Control-Allow-Origin.
Because it's GData, it might however simply be a mistake on your side re. the requested URL: there's no Access-Control-Allow-Origin when you request data in Atom format, only when requesting JSON (and the value is then * which allows everyone, so shouldn't cause any issue like you're seeing): http://code.google.com/p/chromium/issues/detail?id=45980#c2
While this doesn't answer the original question, the following may help others who have the same underlying issue that arrived at this page (I'm using a GWT client with a Groovy web server). This did the trick on the server:
HttpExchange.getResponseHeaders().add("Access-Control-Allow-Origin","*");

gwt: making remote calls fails - sop?

I am writing some interface for users to collect data and send to a server. I went for GWT for various reasons.
Now, when I try to call my server:
String url = "http://127.0.0.1:3000/data/collection.xml";
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
Request request = builder.sendRequest(data, new RequestCallback() {
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
result.setText("SUCCESS!");
} else {
result.setText("ERROR with code: " + response.getStatusCode());
My server gets the request (a POST with some data) but I get ERROR with code: 0 (!) all the time. I guess this has to do with SOP. I read lots about this SOP but I am even more confused now. I tried to follow this tutorial but that's using a different approach (I managed to adapt it to issue GET calls only, but return objects are always null).
Can anyone point me into the right direction? thanks
You can not call any service from another server because of SOP. What you can do, you can use your original server as proxy to other servers.. I would recommend you to read this tutorial.