Getting null in httpResponseMessage when calling REST API using HTTPClient - c#

I am trying to execute a REST API which uses HTTP POST. The API consumes and produces xml content. I am getting null in httpResponseMessage and no exception is thrown. I tried executing the same REST API through HttpWebRequest I am getting the response Below you can find Working and Not Working case. Performance wise HttpWebRequest or HTTPClient which is better ?
Not Working case HTTPClient
try {
using(var client = new HttpClient()) {
Console.WriteLine(inputxml);
var httpContent = new StringContent(inputxml, Encoding.UTF8, "application/xml");
Uri testUri = new Uri("http://127.0.0.1:8080/rest/services/getDocument");
var httpResponseMessage = await client.PostAsync(testUri, httpContent);
Console.WriteLine(httpResponseMessage.Content.ReadAsStringAsync());
if (httpResponseMessage.StatusCode == HttpStatusCode.OK) {
var messageContents = await httpResponseMessage.Content.ReadAsStringAsync();
}
}
}
Working Case HTTPWebREquest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://127.0.0.1:8080/rest/services/getDocument");
byte[] bytes = Encoding.UTF8.GetBytes(inputxml.ToString());
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
Console.WriteLine("responseStr " + responseStr);
return responseStr;
}
return null;
}

Here:
Console.WriteLine(httpResponseMessage.Content.ReadAsStringAsync());
You're reading the response body, and printing System.Threading.Task1` to the console. Then the next time you try to read the response body again, its stream is at its end and won't return anything.
Read the content once, await the call and store the result in a variable.

You can use RestSharp for sending HTTP Requests.
For me, the best and the simplest method for HTTP Communication.
public string SendRequest(String xml)
{
string responseMessage= "";
RestClient restClient;
restClient = new RestClient("http://127.0.0.1:8080/rest/services/getDocument");
RestRequest restRequest = new RestRequest(Method.POST);
restRequest.AddHeader("Accept", "application/xml");
restRequest.AddHeader("Content-Type", "application/xml");
restRequest.AddXmlBody(xml);
IRestResponse restResponse = restClient.Execute(restRequest);
if (restResponse.StatusCode == HttpStatusCode.OK)
{
responseMessage= restResponse.Content;
}
return responseMessage;
}
}

Related

c # desktop app error 401 How can I send the data

I do not have a problem getting tokens in the desktop application I developed. But when I try to send data, I get a 401 error.
HttpWebRequest webRequest;
string requestParams = "";
webRequest = (HttpWebRequest)WebRequest.Create("url");
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.Headers.Add("x-api-key", "112233");
webRequest.Headers.Add("AccessToken", token);
byte[] byteArray = Encoding.UTF8.GetBytes(req);
webRequest.ContentLength = byteArray.Length;
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(byteArray, 0, byteArray.Length);
}
using (WebResponse response = webRequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
string Json = rdr.ReadToEnd();
}
}
The problem is about setting the token. First, you need to be sure what your server wants as a token. You can try this for the Bearer token.
webRequest.Headers.Add("Authorization", "Bearer " + token);
For your program to work, you have to provide an actual URL, not just the string "url", in
webRequest = (HttpWebRequest)WebRequest.Create("url");
Post to api with c # windows form application. oauth2.0
using (var client1 = new HttpClient ())
{
client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token());
client1.DefaultRequestHeaders.Add("x-api-key", "xxxx");
var builder = new UriBuilder(new Uri("you - url"));
HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
request1.Content = new StringContent("{\"values\":" + JsonConvert.SerializeObject("you -data")+ "}", Encoding.UTF8, "application/json");
HttpResponseMessage response = await client1.SendAsync(request1);
};

Convert HttpWebRequest to HttpClient with POST method

I try to convert HttpWebRequest to HttpClient but without success.
Can anybody help me?
It is my simple code with HttpWebRequest:
string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Proxy = null;
request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0";
request.ContentType = "text/x-gwt-rpc; charset=UTF-8";
request.Headers.Add("Cookie", SetCookie);//get it after login
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
I think you can convert you HttpWebRequest based code to HttpClient based like this:
string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
var content = new ByteArrayContent(data);
using var httpHandler = new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false };
using var client = new HttpClient(httpHandler);
client.DefaultRequestHeaders.Add("UserAgent","Mozilla/5.0");
client.DefaultRequestHeaders.Add("ContentType", "text/x-gwt-rpc; charset=UTF-8");
client.DefaultRequestHeaders.Add("Cookie", SetCookie);
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = content };
var response = await client.SendAsync(requestMessage);
var responseText = await response.Content.ReadAsStringAsync();
Remarks:
Instead of writing the Request's Stream manually you can use the ByteArrayContent abstraction for this. (Related SO topic)
In order to set the cookie(s) manually you have to turn-off the default behaviour. You can do this via the HttpClientHandler's UseCookies. (Related SO topic)
To set the headers manually you can use the HttpClient's DefaultRequestHeaders (Related SO topic)
The counterpart of GetResponse is the SendAsync
Instead of reading the Response's Stream manually you can use the HttpContent's ReadAsStringAsync (Related SO topic)
UPDATE: Include OP's amended code
var content = new StringContent(postData, Encoding.UTF8, "text/x-gwt-rpc");
So, instead of ByteArrayContent StringContent is being used.

C# HttpWebRequest "Request Header" in JSON POST

I am translating a JSON API into C# Methods, and I encountered a Problem where the JSON RPC API (POST) says
All other methods require the result from authentication ( = sessionId), either per pathparameter
;jsessionid=644AFBF2C1B592B68C6B04938BD26965
or per cookie (RequestHeader)
JSESSIONID=644AFBF2C1B592B68C6B04938BD26965
My current WebRequest Method:
private async static Task<string> SendJsonAndWait(string json, string url, string sessionId) {
string result;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using(StreamWriter streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync())) {
await streamWriter.WriteAsync(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
Stream responseStream = httpResponse.GetResponseStream();
if(responseStream == null)
throw new Exception("Response Stream was null!");
using(StreamReader streamReader = new StreamReader(responseStream)) {
result = await streamReader.ReadToEndAsync();
}
return result;
}
How do I add the JSESSIONID Parameter to my WebRequest? I am not very familiar with WebRequests, please explain briefly!
Thank you!
Use Cookies.
Your Case would look like this;
private async static Task<string> SendJsonAndWait(string json, string url, string sessionId) {
Uri uri = new Uri(url);
string result;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Add the JSESSIONID Cookie
if(httpWebRequest.CookieContainer == null)
httpWebRequest.CookieContainer = new CookieContainer();
if(!string.IsNullOrWhiteSpace(sessionId))
httpWebRequest.CookieContainer.Add(new Cookie("JSESSIONID", sessionId, "/", uri.Host));
using(StreamWriter streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync())) {
await streamWriter.WriteAsync(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
Stream responseStream = httpResponse.GetResponseStream();
if(responseStream == null)
throw new Exception("Response Stream was null!");
using(StreamReader streamReader = new StreamReader(responseStream)) {
result = await streamReader.ReadToEndAsync();
}
return result;
}
You can add the token directly to your URL :
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create($"{url}?jsessionid={sessionId}");
Or in the headers :
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create({url);
httpWebRequest.Headers["JSESSIONID"] = sessionId;

How to make ordinary WebRequest async and awaitable?

I need to make the following code async and awaitable.
I need to get a lot of data from the web server, and then this data will be used to populate the xaml page in my application.
So, I need the DefLogin() method to be awaitable.
Is it possible?
public void DefLogin()
{
postData = "My Data To Post";
var url = new Uri("Url To Post to", UriKind.Absolute);
webRequest = WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
Debug.WriteLine("Start BEGINGetResponse");
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
public void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
string Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
if (Response == "")
{
//show some error msg to the user
Debug.WriteLine("ERROR");
}
else
{
//Your response will be available in "Response"
Debug.WriteLine(Response);
}
}
catch (WebException)
{
//error
}
}
I saw this question on StackOverflow: Converting ordinary Http Post web request with Async and Await, but I could not understand the answer properly.
Please can anyone help? I would be really grateful!
You can use TaskFactory.FromAsync to convert APM to TAP, making a lot of tiny extension methods like this:
public static Task<Stream> GetRequestStreamAsync(this WebRequest request)
{
return TaskFactory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null);
}
and do the same for WebRequest.GetResponse and (if necessary) Stream.Write, Stream.Flush, etc.
Then you can write your actual logic using async and await without any callbacks:
public async Task DefLoginAsync()
{
postData = "My Data To Post";
var url = new Uri("Url To Post to", UriKind.Absolute);
webRequest = WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
using (Stream postStream = await webRequest.GetRequestStreamAsync())
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
await postStream.WriteAsync(byteArray, 0, byteArray.Length);
await postStream.FlushAsync();
}
try
{
string Response;
using (var response = (HttpWebResponse)await webRequest.GetResponseAsync());
using (Stream streamResponse = response.GetResponseStream())
using (StreamReader streamReader = new StreamReader(streamResponse))
{
Response = await streamReader.ReadToEndAsync();
}
if (Response == "")
{
//show some error msg to the user
Debug.WriteLine("ERROR");
}
else
{
//Your response will be available in "Response"
Debug.WriteLine(Response);
}
}
catch (WebException)
{
//error
}
}

Httpwebrequest--POST method using text file

I am trying to replicate a Couch database using .NET classes instead of a curl command line. I have never used WebRequest or Httpwebrequest before, but I am attempting to use them to make a post request with the script below.
Here is the JSON script for couchdb replication(I know this works):
{ ""_id"":"database_replicate8/7/12", "source":sourcedb, ""target"":"targetDB", ""create_target"":true, ""user_ctx"": { ""roles"": ["myrole"] } }
The above script is put into a text file, sourcefile.txt. I want to take this line and put it in a POST web request using .NET functionality.
After looking into it, I chose to use the httpwebrequest class. Below is what I have so far--I got this from http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
HttpWebRequest bob = (HttpWebRequest)WebRequest.Create("sourceDBURL");
bob.Method = "POST";
bob.ContentType = "application/json";
byte[] bytearray = File.ReadAllBytes(#"sourcefile.txt");
Stream datastream = bob.GetRequestStream();
datastream.Write(bytearray, 0, bytearray.Length);
datastream.Close();
Am I going about this correctly? I am relatively new to web technologies and still learning how http calls work.
Here is a method I use for creating POST requests:
private static HttpWebRequest createNewPostRequest(string apikey, string secretkey, string endpoint)
{
HttpWebRequest request = WebRequest.Create(endpoint) as HttpWebRequest;
request.Proxy = null;
request.Method = "POST";
//Specify the xml/Json content types that are acceptable.
request.ContentType = "application/xml";
request.Accept = "application/xml";
//Attach authorization information
request.Headers.Add("Authorization", apikey);
request.Headers.Add("Secretkey", secretkey);
return request;
}
Within my main method I call it like this:
HttpWebRequest request = createNewPostRequest(apikey, secretkey, endpoint);
and I then pass my data to the method like this:
string requestBody = SerializeToString(requestObj);
byte[] byteStr = Encoding.UTF8.GetBytes(requestBody);
request.ContentLength = byteStr.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(byteStr, 0, byteStr.Length);
}
//Parse the response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//Business error
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine(string.Format("Error: response status code is{0}, at time:{1}", response.StatusCode, DateTime.Now.ToString()));
return "error";
}
else if (response.StatusCode == HttpStatusCode.OK)//Success
{
using (Stream respStream = response.GetResponseStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(SubmitReportResponse));
reportReq = serializer.Deserialize(respStream) as SubmitReportResponse;
}
}
...
In my case I serialize/deserialize my body from a class - you will need to alter this to use your text file. If you want an easy drop in solution, then change the SerializetoString method to a method that loads your text file to a string.

Categories