我使用以下代码使用API将条目发布到Wufoo.com。代码中没有错误,并且正在以正确的形式创建新条目。唯一的问题是新记录的所有字段都是空白的。可以请检查这个代码并告诉我这里我做错了什么。谢谢你的帮助!!!!
string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
string postdata = "{\"Field1\":\"Anshuman\",\"Field3\":\"3216549870\":\"Field4\":\"test@new.com\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);
WebRequest myReq = WebRequest.Create(url);
string username = "3QNW-MXE2-O74M-RUC6";
string password = "mfs802r0uokbfd";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
myReq.ContentType = "multipart/form-data";
myReq.Method = "POST";
myReq.ContentLength = byteArray.Length;
Stream dataStream = myReq.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);
答案 0 :(得分:2)
试试这个应该适合你的代码片段。
string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
string APIKey = "3QNW-MXE2-O74M-RUC6";
string Hashcode = "mfs802r0uokbfd";
string usernamePassword = APIKey + ":" + Hashcode;
// Prepare web request...
HttpWebRequest myReq =
(HttpWebRequest)WebRequest.Create(url);
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(APIKey, Hashcode));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Field1=Anshuman&Field3=3216549870&Field4=test@new.com";
byte[] data = encoding.GetBytes(postData);
myReq.Method = "POST";
myReq.ContentType = "application/x-www-form-urlencoded";
myReq.ContentLength = data.Length;
Stream newStream = myReq.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
干杯!!这会让你的工作成为你想要的......