When unit testing, I want to check csv formatted results, so I have the following code in my test.
MyDtoReq request = new MyDtoReq();
// ... assign some properties
string url = request.ToUrl("GET");
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
httpReq.Accept = "text/csv";
csv = new StreamReader(httpReq.GetResponse().GetResponseStream()).ReadToEnd();
That works fine, if the request succeeds. But when it fails, it raises a System.Net.WebException that doesn't have the expected WebServiceException.ResponseStatus details. NUnit reports the exception as follows:
Test Name: TestReq
Test FullName: [...].TestReq
Test Source: c:\Users\[...]\UnitTestProject1\ServiceTests.cs : line 261
Test Outcome: Failed
Test Duration: 0:00:27.104
Result Message: System.Net.WebException : The remote server returned an error: (400) Bad Request.
Result StackTrace: at [...].TestReq() in c:\Users\[...]\UnitTestProject1\ServiceTests.cs:line 287
Turns out that this is by design, as most clients requesting csv format are not able to parse a ResponseStatus. In order to see the actual error, I would re-submit the request with format=html in the browser - a frustrating waste of time.
Here's how to get the actual error message from failing csv format requests:
// Declared in test setup
public const string Host = "http://localhost:1337";
private const string BaseUri = Host + "/";
[Test]
public void TestMyDtoReqCsvFormat()
{
MyDtoReq request = new MyDtoReq();
request.startDate = "20130919";
request.endDate = "20130930";
request.source = "Token";
try
{
string requestUrl = request.ToUrl("GET");
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(requestUrl);
httpReq.Accept = "text/csv";
var csv = new StreamReader(httpReq.GetResponse().GetResponseStream()).ReadToEnd();
// assert some facts about the contents of csv
}
catch (Exception)
{
try {
JsonServiceClient client = new JsonServiceClient(BaseUri);
MyDtoReqResponse response = client.Get(request);
// do something if re-request succeeds (i.e. was a transient error)
}
catch (WebServiceException webEx)
{
var message = webEx.ResponseStatus.ErrorCode +
" " + webEx.ResponseStatus.Message.Trim() +
" " + webEx.ResponseStatus.StackTrace.Trim();
throw new WebException(message,webEx);
}
catch (Exception otherEx) {
System.Diagnostics.Debug.WriteLine(otherEx.Message);
throw new Exception(otherEx.Message, otherEx);
}
}
}
Related
I am getting below error when i am trying to print response from http connection. I think i am using the stream twice and for this reason it says stream is closed but not sure.
The error is coming on the writeInstFile() method where i am trying to print response using getInputStream().getText()
I just wanted to create new string variable and put the connection stream response in that variable and print it so that i can see what response i am getting when i try to do http request:
private void writeInstFile(File outFile) {
Properties config = workflowEnvironment.getConfig()
//http connection to get Authentication token
HttpURLConnection connection = connection("https://XXX/RequestToken")
if (connection.responseCode == HTTP_OK) {
String authToken = authTokenFromJson(jsonFromDssRequestTokenConnection(connection))
log.info("Authentication token: $authToken")
println(connection.getInputStream().getText());
if (connection.responseCode == HTTP_OK) {
println("Got http response code: ${connection.responseCode}, message: ${connection.responseMessage}")
log.info("Successful DSS request to ${connection.getURL()}")
LazyMap json = jsonFromDssExtractionConnection(connection)
.....
}
private static String authTokenFromJson(LazyMap json) {
json.value
}
private static LazyMap jsonFromDssRequestTokenConnection(HttpURLConnection connection) {
connection.inputStream.withCloseable { inStream ->
new JsonSlurper().parse(inStream as InputStream)
} as LazyMap
}
I would simply like to post some json to an api from a background task in my UWP app and get back the response to read it out. My background task is constantly failing with
Platform::DisconnectedException ^ at memory location 0x077FEE74.
I tried many ways to get it work with things from the internet but only slightly adjusting the failure. Without the code I can execute the background task perfectly.
Here the code:
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
StringContent theContent = new StringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Encoding.UTF8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, theContent);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
// show the response status code
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
The Json I am trying to imitate looks something like this:
{"keep_login":null,"id":null,"username":"zumbauser","password":"zumbapw"}
Any help is appreciated!
It would be preferrable to use windows.web.http.httpclient for UWP apps since it supports wide range of Languages. See table from the reference
Now for StringContent. Windows.Web.HttpClient as HttpStringContent which is similar to StringContent in System.Net.Http.HttpClient
This is a snippet for example but make sure you read the reference.
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(theUri, content);
So to complete your Method it will be
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, content);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
Note: I tried the same with one of my APP in a background task and it works fine. only case it failed was when i tried to send huge data i got Not enough memory available error.
Good Luck and Happy Coding.
I know this might sound silly, but which HttpClient are you using? The one from System.Net.Http or the one from Windows.Web.Http? Try switching to the other one, it might help
I am building my first windowsPhone 8.1 application ,the role of my application is to create connection with server to get information from it, so I am writing the code to do this process by sending json-rpc request to server to get some information ,I am successful to get it in first time but when I send the second request I am receiving an empty response with 404 error (page not found).
But when I call the service without https (http only) it works fine regardless how many time I call it !
public async Task<string> GetDataFromServer(string urlToCall, string JSONData,string RR)
{
string UserName = “XXXXXXX”
string Password = "XXX";
using ( var handler = new HttpClientHandler())
{
handler.Credentials = new NetworkCredential(UserName, Password);
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = null;
try
{
response = await client.PostAsync(urlToCall, new StringContent(JSONData.ToString(), Encoding.UTF8, " application/json"));
string res = response.Content.ReadAsStringAsync().Result;
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog(res);
await g.ShowAsync();
return res;
}
catch (Exception ex)
{
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog("Error is : " + ex.Message);
g.ShowAsync();
return "Error";
}
finally
{
response.Dispose();
client.CancelPendingRequests();
client.Dispose();
handler.Dispose();
}
}
}
Again, when call the URL of service (start with https) on first time I got response with seeked data, but second time I receive an empty response with 404 error (page not found) !!
Any help please
Please try to use this solution.
public async Task<string> SendJSONData3(string urlToCall, string JSONData)
{
string UserName = "XXXXXXXXX";
string Password = "XXXXXXXXX";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlToCall);
httpWebRequest.Credentials = new NetworkCredential(UserName, Password);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync()))
{
string json = JSONData;
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
A couple of ideas:
Do not use the .Result property. Just use await instead to avoid deadlocks.
Remove the additional space in front of the media type parameter " application/json"
Enable logging on the webserver and see if the second request arrives on the server.
Get a network trace, for example with Wireshark or Fiddler.
Try puting WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); in your initialization code, as proposed in this answer.
hello i'm write a http web server in my app.
I used this code
http://developer.nokia.com/community/wiki/A_simplistic_HTTP_Server_on_Windows_Phone
this procedure works but not have a extentions of file in http response
return a file name without extention (.zip)
private async Task<StringBuilder> HandleRequest(StreamSocket socket)
{
//Initialize IO classes
DataReader reader = new DataReader(socket.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
DataWriter writer = new DataWriter(socket.OutputStream);
writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
//handle actual HTTP request
String request = await StreamReadLine(reader);
string[] tokens = request.Split(' ');
if (tokens.Length != 3)
{
throw new Exception("invalid http request line");
}
string httpMethod = tokens[0].ToUpper();
string httpUrl = tokens[1];
//read HTTP headers - contents ignored in this sample
while (!String.IsNullOrEmpty(await StreamReadLine(reader))) ;
try
{
if (httpUrl == "DOWNLOADZIP")
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
String content = null;
Stream stream;
byte[] data;
IsolatedStorageFileStream sri = storage.OpenFile("CompressedFiles.zip", FileMode.Open, FileAccess.Read);
if (null != sri)
{
stream = sri;
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
writer.writebytes(data);
}
}
}
}
catch (Exception ex)//any exception leads to an Internal server error
{
writer.WriteString("HTTP/1.0 500 Internal server error\r\n");
writer.WriteString("Connection: close\r\n");
writer.WriteString("\r\n");
writer.WriteString(ex.Message);
}
}
await writer.StoreAsync();//write data actually to the network interface
socket.Dispose();
return null;
}
this is a solutions
ret.AppendLine("HTTP/1.0 200 OK");
ret.AppendLine("Content-Type: text/html");
ret.AppendLine("Connection: close");
ret.AppendLine("");
ret.AppendLine("Content-Disposition: attachment; filename=myfile.zip");
I want upload a file (any type) on a server.
I have my file which is saved like this (I use FileAssociation)
await SharedStorageAccessManager.CopySharedFileAsync(ApplicationData.Current.LocalFolder, "fileToSave" + fileext, NameCollisionOption.ReplaceExisting, NavigationContext.QueryString["filetoken"]);
Then I get the saved file
StorageFolder folder = ApplicationData.Current.LocalFolder;
var file = await folder.GetFileAsync("fileToSave" + fileext);
Stream data = Application.GetResourceStream(new Uri(file.Path, UriKind.Relative)).Stream;
string filename = System.IO.Path.GetFileName(file.Path);
ServerFunctions.UploadFile(filename,data);
Then I start the Upload
internal void UploadFile(string fileName,Stream data)
{
WebClient web = new WebClient();
if (!string.IsNullOrEmpty(dataRequestParam.AuthentificationLogin))
{
System.Net.NetworkCredential account = new NetworkCredential(dataRequestParam.AuthentificationLogin, dataRequestParam.AuthentificationPassword);
web.Credentials = account;
}
web.AllowReadStreamBuffering = true;
web.AllowWriteStreamBuffering = true;
web.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
web.OpenWriteAsync(dataRequestParam.TargetUri,"POST");
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
The web server is supposed to send me as a response a xml with an error code or succes code inside.
None error is thrown but it doesnt work.And I don't understand why the e.result is a stream object. As I said the server should return a string...(xml file)
Could you bring me some explannations of what is happening in my code and if it will work with all types of files ?
Thanks
I think part of the problem here is that you're attempting to get this to behave like a streaming protocol when it seems you intend a request/response type architecture. For those purposes, you should consider working with a WebRequest object.
Bear with me as I fully qualify the namespace of the objects used inline, so it may get a little verbose, but I want you to know where to find these things.
internal async void UploadFile(string fileName, System.IO.Stream data)
{
// Specify URI, method, and credentials for the request
System.Net.WebRequest web = System.Net.HttpWebRequest.CreateHttp(dataRequestParam.TargetUri);
web.Method = "POST";
if (!string.IsNullOrEmpty(dataRequestParam.AuthenticationLogin))
{
web.Credentials = new System.Net.NetworkCredential(dataRequestParam.AuthenticationLogin, dataRequestParam.AuthenticationPassword);
}
// Create the request payload from the provided stream
System.IO.Stream requestStream =
await System.Threading.Tasks.Task<System.IO.Stream>.Factory.FromAsync(web.BeginGetRequestStream, web.EndGetRequestStream, null);
await data.CopyToAsync(requestStream);
// Get a response from the server
System.Net.WebResponse response =
await System.Threading.Tasks.Task<System.Net.WebResponse>.Factory.FromAsync(web.BeginGetResponse, web.EndGetResponse, null);
// Possibly parse the response with an XmlReader (example only)
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(response.GetResponseStream());
string responseText = reader.ReadInnerXml(); // TODO: Real work here
}
The one oddity here is using the Task factory to create a task from the begin and end methods from getting both the request stream and the response. This makes it much simpler to consume these methods as you get a Task back which can be awaited for its return object, which you can then manipulate directly.
I'm not sure what form your response from the server takes on success versus failure, so I've simply shown how to create an XML reader to parse XML from the resulting stream. You can do whatever parsing is necessary yourself on these lines, but this should at least give you a look at what your server is returning in response.
The final code I use.
WebRequest web = HttpWebRequest.CreateHttp(dataRequestParam.TargetUri);
web.ContentType = dataRequestParam.ContentType;
web.Method = "POST";
web.ContentLength = data.Length;
if (!string.IsNullOrEmpty(dataRequestParam.AuthentificationLogin))
{
web.Credentials = new NetworkCredential(dataRequestParam.AuthentificationLogin, dataRequestParam.AuthentificationPassword);
}
using (var requestStream = await Task<Stream>.Factory.FromAsync(web.BeginGetRequestStream, web.EndGetRequestStream, web))
{
await data.CopyToAsync(requestStream);
}
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(web.BeginGetResponse, web.EndGetResponse, web);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}