[WebInvoke(Method = "PUT", UriTemplate = "users/{username}")]
[OperationContract]
void PutUser(string username, User newValue);//update a user
I have a update user method defined as showed above. Then I use a HttpWebRequest to test the method, but how can I pass the User object with this HttpWebResquest?
The following code is what I got so far.
string uri = "http://localhost:8080/userservice/users/userA";
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = "PUT";
req.ContentType = " application/xml";
req.Proxy = null;
string uri = "http://localhost:8080/userservice/users/userA";
string user = "<User xmlns=\"http://schemas.datacontract.org/2004/07/RESTful\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DOB>2009-01-18T00:00:00</DOB><Email>userA#example.com</Email><Id>1</Id><Name>Sample User</Name><Username>userA</Username></User>";
byte[] reqData = Encoding.UTF8.GetBytes(user);
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = "POST";
req.ContentType = " application/xml";
req.ContentLength = user.Length;
req.Proxy = null;
Stream reqStream = req.GetRequestStream();
reqStream.Write(reqData, 0, reqData.Length);
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
string code = resp.StatusCode.ToString();
//StreamReader sr = new StreamReader( resp.GetResponseStream());
//string respStr = sr.ReadToEnd();
Console.WriteLine(code);
Console.Read();
I found the solution, I need to construct the xml string I want to pass and then write it into stream
In WCF/REST you don't pass an object, you pass a message.
If I were doing this, as a first step, I would create a WCF client that interacts with the service. I would examine the messages passed on the wire by the WCF client, and then I'd replicate that message with the HttpWebRequest.
Related
I'm trying to send a POST request to the Livecoin API. I have ensured that every parameter and the encoding is correct, but I keep getting a weird response:
{"success":false,"exception": "Unknown currency pair [currencyPair={1}]|null"}
This is what I'm trying to post:
string response = PrivatePostQuery("exchange/buymarket", "currencyPair=BTC/USD&price=12&amount=12");
And this is the method:
public string PrivatePostQuery(string requestUrl, string parameters = "")
{
parameters = http_build_query(parameters);
string Sign = HashHMAC(this.Exchange.ExchangeConnection.ApiSecretKey, parameters).ToUpper();
string uri = this.Exchange.ExchangeConnection.ApiUrl + requestUrl + "?" + parameters;
byte[] bytes = Encoding.UTF8.GetBytes(parameters);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.Headers["Api-Key"] = this.Exchange.ExchangeConnection.ApiKey;
request.Headers["Sign"] = Sign;
Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
try
{
WebResponse WebResponse = request.GetResponse();
dataStream = WebResponse.GetResponseStream();
StreamReader StreamReader = new StreamReader(dataStream);
return StreamReader.ReadToEnd();
}
catch (WebException ex)
{
return new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
}
I have succeeded getting tickers and my balance from the API, so the problem is not because of the signature or headers.
I have tried changing the request to upper / lower case, and adding the parameters as request headers, with and without them in URL.
Thanks for the help!
Ok so there was no problem at all, LiveCoin exchange doesn't work with BTC-USDT pair, only with BTC-USD. So the response was correct, even tough it was a bit messy.
I have developed a REST WCF service method as following:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Details")]
DetailData GetDetails(TestData requst);
[DataContract]
public class TestData
{
[DataMember]
public string DetailData { get; set; }
}
Now I am trying to invoke the service using following client code:
ASCIIEncoding encoding = new ASCIIEncoding();
string testXml = "<TestData>" +
"<DetailData>" +
"4000" +
"</DetailData>" +
"</TestData>";
string postData = testXml.ToString();
byte[] data = encoding.GetBytes(postData);
string url = "http://localhost/WCFRestService.svc/bh/Details";
string strResult = string.Empty;
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "POST";
// set content type
webrequest.ContentType = "text/xml";
// set content length
webrequest.ContentLength = data.Length;
// get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
//Gets the response
WebResponse response = webrequest.GetResponse();
//Writes the Response
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
string s = sr.ReadToEnd();
I am getting the following error :
"The remote server returned an error: (400) Bad Request"
I could successfully call another service method where "GET" verb is being used. But the above client code for invoking the service using "POST" verb is not working. I think, I am missing something in Client code.
What could be the problem?
Try changing
WebMessageBodyStyle.WrappedRequest
to
WebMessageBodyStyle.Bare
[WebInvoke(Method="POST",UriTemplate="/Users",RequestFormat=WebMessageFormat.Json)]
public string StudentUsers(Student user)
{
return string.Format("Hello {0}", user.Name);
}
Above code is my REST service. And my client code is :
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
Student user =
new Stuent {
Name="Test User",
Email = "test#test.com",
Password = "test"
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(user.GetType());
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, user);
String json = Encoding.UTF8.GetString(ms.ToArray());
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
My service is hosted and I'm using webHttpBinding. When I debug my REST service I'm receiving null in Student object. I am sure that my post method is sending data as I test it by taking Name, Email and Password as parameters at REST service so my data is posted successfully but the thing is my Json data which is posted is not getting converted to Student object. I read somewhere that RESTfull Service will convert that Json data to object. Is that true or we need to convert it explicitly?
You need to sent the Content-Length for your POST-ed data:
request.ContentLength = ms.Length;
see here for more details:
I have fixed the issue by changing WebInvoke to
[WebInvoke(Method="POST",UriTemplate="/Users",RequestFormat=WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
I spent too much hours to overcome this, till now with no success
From my site which is developed in MVC im trying to send a login request to a remote site, for example, facebook.
From fiddler It seems that the following inputs are required charset_test, lsd, locale,email, pass.
lsd key seems to be the unique token
Here is my code
CookieContainer cookies = new CookieContainer()
private string GetToken()
{
Uri uri = new Uri("http://www.facebook.com");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();
HtmlDocument document = new HtmlDocument(); --> HtmlAgilePack object
document.LoadHtml(htmlText);
//Need to check xpath doesn't return null object
HtmlNode node = document.DocumentNode.SelectNodes("//input[#name='lsd']").First();
return node.Attributes["Value"].Value;
}
private void Login()
{
string postData = string.Format("charset_test=fixcharset_test&lsd={0} &locale=en_US&email=myemail&pass=mypass", HttpUtility.UrlEncode(GetToken()));
Uri uri = new Uri("http://www.facebook.com/login.php");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.CookieContainer = cookies;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Method = "POST";
request.AllowAutoRedirect = true;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
//After login to facebook suppose to
public string void AddFriend()
{
Uri uri = new Uri("http://www.facebook.com");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string status = response.StatusDescription;
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseDat
}
Keep in mind, that facebook is just an example, Im aware of its API, Im trying to login to site with no API.
sadly, the response return a facebook page with a sorry message for unrecognizing my browser.
Any help will be more than appreciate.
Thanks,
Are you setting HTTP_USER_AGENT on your request?
I have a WCF Service declared as follows:
[OperationContract, XmlSerializerFormat]
[WebInvoke(UriTemplate = "ProessUpload",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml)]
void ProcessUpload(ProductConfig stream);
I am trying to call this service using WebClient but I am always getting a response 400 (BadRequest) from the server. However if I use HttpWebRequest the WCF consumes my post and correctly responds with 200. I am also able to successfully construct a request using Fiddler to call the WCF Service.
WebClient Code
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/xml");//; charset=utf-8
try
{
string result = webClient.UploadString("http://jeff-laptop/SalesAssist.ImageService/Process", "POST", data2);
}
catch (Exception ex)
{
var e = ex.InnerException;
}
HttpWebRequest code
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://jeff-laptop/SalesAssist.ImageService/Process");
request.ContentType = "application/xml";
request.Method = "POST";
request.KeepAlive = true;
using (Stream requestStream = request.GetRequestStream())
{
var bytes = Encoding.UTF8.GetBytes(data2);
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
var response = (HttpWebResponse)request.GetResponse();
var abc = new StreamReader(response.GetResponseStream()).ReadToEnd();
The XML that is being sent
var data2 = #"<Product><Sku>3327</Sku><NameProduct</Name><Category>Bumper</Category><Brand Id='3'><Collection>14</Collection></Brand></Product>";
Why is that HttpWebRequest works and WebClient doesn't? I can't see a real difference in the sent headers via Fiddler.
Try setting the Encoding property on the WebClient before you send the string. Since you aren't specifying it I suspect it defaults to ASCII. Quoting from the UploadString reference page.
Before uploading the string, this
method converts it to a Byte array
using the encoding specified in the
Encoding property. This method blocks
while the string is transmitted. To
send a string and continue executing
while waiting for the server's
response, use one of the
UploadStringAsync methods.
webClient.Encoding = Encoding.UTF8;