HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
Request.Headers.Add("Authorization", "OAuth " + GetAccessTokenBeta());
Request.Proxy.Credentials = CredentialCache.DefaultCredentials;
Request.Method = "POST";
Request.ContentType = "application/xml";
using (var streamWriter = new StreamWriter(Request.GetRequestStream()))
{
string xml = getXml(tabletype, values.ToArray());
streamWriter.Write(xml);
streamWriter.Flush();
streamWriter.Close();
}
try
{
using (WebResponse response = Request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
}
}
}
catch (WebException ex)
{
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
Core.ShowError("Error connecting to the webservice." + "\r\n" + resp);
}
I have confirmed that the endpoint and XML work using Postman, but I am running into this issue in C#.
Error sending HTTP request. Message payload is of type: BufferInputStream
Related
I have the following code that works when I use it in the unity editor but it doesnt work after I build it using webgl.
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + "/user/create");
req.ContentType = "application/json";
req.Method = "POST";
req.Headers.Add("Access-Control-Allow-Credentials", "true");
req.Headers.Add("Access-Control-Allow-Origin", "*");
req.Headers.Add("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time,Authorization");
req.Headers.Add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
//req.Headers.Add("content-type", "text/json");
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(json);
}
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
using (var streamReader = new StreamReader(res.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (WebException ex)
{
Debug.LogWarning("Error: " + ex.Message);
}
It gives me the following error
public void processVoucher()
{
try
{
string url = "http://192.168.xxx.xx:xxxx/context-root-xxxxxxxx/AccountsPayableManagerPort?WSDL/processVoucher";
StreamReader str = new StreamReader(#"F:\IntelliChief integration to JD Edwards for AP Invoice entry\processVoucher_input_payload.xml");
string ipParameter = str.ReadToEnd();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "application/xml";
req.KeepAlive = true;
req.Timeout = 30000;
req.Accept = "application/xml";//"text/xml";
req.Headers.Clear();
req.Method = "POST";
Encoding encode = Encoding.GetEncoding("utf-8");
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(ipParameter);
}
}
var response = req.GetResponse(); // here i am getting Unsupported Media Type issue
Stream responseStream = response.GetResponseStream();
StreamReader strReader = new StreamReader(responseStream, encode, true);
string result = strReader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("Error Message:" + ex.Message);
throw;
}
}
I got requirement of consuming web service, display the result, i am trying to consume web service by using HttpWebRequest class. I running exception in req.GetResponse() any help is appreciated.
public void processVoucher()
{
string soap = null;
try
{
StreamReader str = new StreamReader(#"F:\xxx\some.xml");
soap = str.ReadToEnd();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.xxx.xx:xxxx/bla-bla-bla/AccountsPayableManagerPort?WSDL");
req.ContentType = "text/xml;charset=\"UTF-8\"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
using (WebResponse response = req.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
}
}
}
catch (Exception)
{
throw;
}
}
Finally i found solution to the issue, above is the working code. After i changed req.ContentType = "application/xml"; to req.ContentType = "text/xml;charset=\"UTF-8\""; , req.Accept = "application/xml"; to req.Accept = "text/xml"; and i removed req.Headers.Clear(); my code started working thanks all for your support...
While using the below code to make a call to the SOAP, I am getting the "Unauthorized" as a response.
NetworkCredential credentials = new NetworkCredential("username", "password");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = credentials;
try
{
WebResponse response = request.GetResponse();
using(Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch(WebException Ex)
{
WebResponse errorResponse = Ex.Response;
using(Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string errorText = reader.ReadToEnd();
}
throw;
}
Can you please correct me where I am wrong?
I am getting the error :- "The remote server returned an error: (401) Unauthorized."
This should work
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
string authInfo = "username" + ":" + "Password";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers[HttpRequestHeader.Authorization] = "Basic " + authInfo;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (var responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
This is not an error. It is a warning about your credential. You should send legal user credential to get a response to your end-point. Please check-out this;
NetworkCredential credentials = new NetworkCredential{
Username=//fill your username,
Password=//fill your password but be carefull, is it hashed or not?
};
var request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = credentials;
try
{
WebResponse response = request.GetResponse();
using(Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch(WebException Ex)
{
WebResponse errorResponse = Ex.Response;
using(Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string errorText = reader.ReadToEnd();
}
throw;
}
I am calling an API but its giving me below error:
UriFormatException: Absolute URI is too short
Please check below code:
void updatePlayer ()
{
HttpWebRequest httpWebRequest = WebRequest.Create (requestURL) as HttpWebRequest;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add ("x-api-key", "e1Zdd1YD92aJtpyDaCtSDTZCrDXtv2c9Cf");
string json = "{" +
"'firstName': '100'," +
"'lastName': 'DEF'" +
"}";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
streamWriter.Write (json);
}
httpWebRequest.ContentLength = json.Length;
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse ();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
var responseText = streamReader.ReadToEnd ();
//Now you have your response.
//or false depending on information in the response
Debug.Log (responseText);
}
}
I want to use google api for creation of gmail user account. I am sending JSON request to server for getting authorization code but I got these error in httpwebresponse :-
Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request
var request = (HttpWebRequest)WebRequest.Create(#"https://accounts.google.com/o/oauth2/auth");
request.Method = "POST";
request.ContentType = "text/json";
request.KeepAlive = false;
//request.ContentLength = 0;
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"scope\":\"https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile\"," + "\"state\":\"%2Fprofile\"," + "\"redirect_uri\":\"http://gmailcheck.com/response.aspx\"," + "\"response_type\":\"code\"," + "\"client_id\":\"841994137170.apps.googleusercontent.com\"}";
streamWriter.Write(json);
// streamWriter.Flush();
//streamWriter.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
StreamReader responsereader = new StreamReader(response.GetResponseStream());
var responsedata = responsereader.ReadToEnd();
//Session["responseinfo"] = responsereader;
//testdiv.InnerHtml = responsedata;
}
}
As soon as you get an exception, you have to read the actual responce from server there should be something helpfull. Like an error description or extended status code...
For Instance:
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
... your code goes here....
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
var httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream())
{
StreamReader sr = new StreamReader(data);
throw new Exception(sr.ReadToEnd());
}
}
}